0% found this document useful (0 votes)
11 views3 pages

Python For Og Lecture 66 - Map Function

The document discusses different methods to calculate pressure at a depth of 3000 feet for a list of mud weights including using a for loop, creating a function, list comprehension, and the map function. The map function applies a function to each item in an iterable like a list without needing an explicit for loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Python For Og Lecture 66 - Map Function

The document discusses different methods to calculate pressure at a depth of 3000 feet for a list of mud weights including using a for loop, creating a function, list comprehension, and the map function. The map function applies a function to each item in an iterable like a list without needing an explicit for loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2/3/2021 Python for O&G Lecture 66: Map Function - Colaboratory

Python for Oil and Gas

Website - https://petroleumfromscratchin.wordpress.com/

LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch

YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw

# we have a list of different mud weights in ppg

# and given that we have a depth of 3000 ft

# we have to give out a list of pressures at 3000 ft for diff mud wts

mud_wt = [8.5, 10.4, 14.5, 12.1, 9.54]

# we have different methods to do the same problem

# method 1 - with for loop

pres = []

for i in mud_wt: /
2/3/2021 Python for O&G Lecture 66: Map Function - Colaboratory

pres.append(0.052*i*3000)

print(pres)

[1326.0, 1622.3999999999999, 2262.0, 1887.6, 1488.2399999999998]

# method 2 - create a function

def pres_calc(l):
pres_new = []
for i in l:
pres_new.append(0.052*i*3000)
return pres_new

pres_calc(mud_wt)

[1326.0, 1622.3999999999999, 2262.0, 1887.6, 1488.2399999999998]

# method 3 -> list comprehension

pressure = [0.052*i*3000 for i in mud_wt]


print(pressure)

[1326.0, 1622.3999999999999, 2262.0, 1887.6, 1488.2399999999998]

# method 4 (map function)

# Syntax -->> map(function_name, iterable such as list tuple dictionary)

# Functionality: This function will be applicable to each item of the iterable. Means here we'll not have to apply for loop

# we need a function, so first lets crate a function

print(mud_wt)

/
2/3/2021 Python for O&G Lecture 66: Map Function - Colaboratory

[8.5, 10.4, 14.5, 12.1, 9.54]

def func(m):
return 0.052*m*3000

tuple(map(func, mud_wt ))

(1326.0, 1622.3999999999999, 2262.0, 1887.6, 1488.2399999999998)

# you need not create a function differently, we can do it by using lambda function

list(map(lambda m: m*0.052*3000, mud_wt))

[1326.0, 1622.3999999999999, 2262.0, 1887.6, 1488.2399999999998]

You might also like