You are on page 1of 15

2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - 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

Lecture 36: Methods in lists

count method
sort method
sorting function
clear method
split method
join method

count method /
2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory

# count method

# Syntax - list_name.count(value or character you want to count)

# porosity of different reservoirs

phi = [0.2, 0.3, 0.25, 0.4, 0.2, 0.35, 0.31, 0.30, 0.2]

phi.count(0.2)

phi.count(0.30)

alpha = ['a', 'b', 'f', 'o', 65, 'b', 'b', 'c', 'a']

alpha.count(65)

sort method

# sort method

# list_name.sort()

# sort the list phi

phi = [0.2, 0.3, 0.25, 0.4, 0.2, 0.35, 0.31, 0.30, 0.2]
/
2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory

phi.sort()

print(phi)

[0.2, 0.2, 0.2, 0.25, 0.3, 0.3, 0.31, 0.35, 0.4]

# it can also sort alphabets

# sort the list alpha

alpha = ['a', 'b', 'f', 'o', 'b', 'b', 'c', 'a']

alpha.sort()

print(alpha)

['a', 'a', 'b', 'b', 'b', 'c', 'f', 'o']

sorting function

# sorting function

# --> sort method will sort the original list


# --> sorting function will sort only for once to show you how sorted list will look like. it won't change the original list

# make any list here

num = [1, 5, 89, 57, 101, 20, 37]

sorted(num)

[1, 5, 20, 37, 57, 89, 101] /


2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory

print(num)

[1, 5, 89, 57, 101, 20, 37]

clear method

# clear method

# clears the list. Makes the list empty

# syntax - list_name.clear()

print(num)

[1, 5, 89, 57, 101, 20, 37]

num.clear()

print(num)

[]

copy method

# copy method

# creates a copy. Syntax - list_name.copy()

abc = [1, 2, 'pyton', 'Divyansh']

abc_new = abc.copy() /
2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory

print(abc_new)

[1, 2, 'pyton', 'Divyansh']

split method

# split method

# we have already seen this split method in input function

# Split method actually converts the string to a list

# for example ask first name and last name of user. store them in two different variables

first, last = input('Please enter your first name and last name: ').split()

Please enter your first name and last name: Divyansh Sethi

print(first)
print(last)

Divyansh
Sethi

full_name = input('Please enter your first name and last name: ').split()

Please enter your first name and last name: Divyansh Lokesh Sethi

print(full_name)

['Divyansh', 'Lokesh', 'Sethi']

# this time store them in a single variable


/
2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory

join method

# join method ----> Convert lists to string --> opposite of split

# syntax - (' ').join(name of list you want to convert into s string)

(', ').join(full_name)

'Divyansh, Lokesh, Sethi'

Lecture 37: Loops inside lists, lists in lists

Loops in list

phi = [0.2, 0.3, 0.25, 0.4, 0.2, 0.35, 0.31, 0.3, 0.2]

len(phi)

# while loop in lists

i = 0 /
2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory

while i<len(phi): # index i < 9


print(phi[i]) # phi[0], phi[1]
i = i + 1

0.2
0.3
0.25
0.4
0.2
0.35
0.31
0.3
0.2

# for loops inside list

phi = [0.2, 0.3, 0.25, 0.4, 0.2, 0.35, 0.31, 0.3, 0.2]

for i in phi:
print(i)

0.2
0.3
0.25
0.4
0.2
0.35
0.31
0.3
0.2

Lists inside list


/
2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory

# [res__1_info, res_2_info, res_3_info]

# [por, perm, payzone height] - This is how a list for one reservoir will look like

res_1 = [0.2, 25, 40]


res_2 = [0.15, 30, 55]
res_3 = [0.30, 35, 60]

info = [[0.2, 25, 40], [0.15, 30, 55], [0.30, 35, 60]]

len(info)

# indexing for these lists

info = [[0.2, 25, 40], [0.15, 30, 55], [0.30, 35, 60]]

info[1]

[0.15, 30, 55]

info[0][1]

25

info[2][2]

60

# looping inside these lists /


2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory

info = [[0.2, 25, 40], [0.15, 30, 55], [0.30, 35, 60]]

for i in info:
print(i)

[0.2, 25, 40]


[0.15, 30, 55]
[0.3, 35, 60]

for i in info: # values of i - [0.2, 25, 40], [0.15, 30, 55], [0.3, 35, 60]
for j in i:
print(j)

0.2
25
40
0.15
30
55
0.3
35
60

Generating lists with range functions

# create a list of natural numbers using range function

ab = list(range(1, 100))

print(ab)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 4

# Create a list of even numbers starting from 2 till 12

/
even = list(range(2 14 2))
2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory
even = list(range(2, 14, 2))

print(even)

[2, 4, 6, 8, 10, 12]

Code Text

Lecture 38: Passing list inside a function

Passing list inside a function

# I want to prepare a function which converts all the items of list into negative

def negative(a):
neg_items = [] # -12, -1, .....-20
for i in a: # a = nums = [12, 1, 45, 23, 54, 20] i = 12, 1
neg_items.append(-i)
return neg_items

negative(nums)

[-12, -1, -45, -23, -54, -20]

jkl = [12, 4, 98]

negative(jkl)
/
2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory

[-12, -4, -98]

Excercises

Exercise 1

# 1 I have given you a list of permeabilities in md

perm = [10, 14, 20, 25, 33, 35 ,40, 45]

# create a function such that we get a output as a list which shows values as Even or odd

def even_odd(b):
EoO = []
for i in b: # 10, 14, 20, 25.........45
if i%2 == 0:
EoO.append('Even')
else:
EoO.append('Odd')
return EoO

even_odd(perm)

['Even', 'Even', 'Even', 'Odd', 'Odd', 'Odd', 'Even', 'Odd']

perm_2 = [12, 45, 40]


/
2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory

ev_od = []

for i in perm_2:
if i%2 == 0:
ev_od.append('Even')
else:
ev_od.append('Odd')

print(ev_od)

['Even', 'Odd', 'Even']

even_odd(perm_2)

['Even', 'Odd', 'Even']

Excercise 2

# I have given you a list of porosities in percentage

por = [10, 12, 16, 24, 31, 27]

# You have to crate a function which returns us the list with these porosities values in fraction

def fraction(p):
por_frac = []
for i in p:
por_frac.append(i/100)
return por_frac

fraction(por)

[0.1, 0.12, 0.16, 0.24, 0.31, 0.27]


/
2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory

por_2 = [15, 45, 89]

fraction(por_2)

[0.15, 0.45, 0.89]

Assignment 12

# make a list using range function


# list should hava values 5, 10, 15, 20, 25, 30
# store this list as s_w (saturation of water) s_w = [5, 10, 15, 20, 25, 30]

# Given that saturation of gas is 10 %

# mak a function that returns us the list of sat of oil for each saturation of water

Lecture 39: Solution of Assignment 12

Solution for Assignment 12

s_w = list(range(5, 35, 5))

print(s_w)

[5, 10, 15, 20, 25, 30]

/
def saturation oil(a):
2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory
def saturation_oil(a):
s_o = []
for i in a:
s_o.append(100-i-10)
return s_o

saturation_oil(s_w)

[85, 80, 75, 70, 65, 60]

Lecture 40: Assignment 13

Assignment 13

# mud densities filter them into lists i.e. one for mud wt less than 12 and other for more than 12.

mud_wt = [9.4, 10.5, 14.1, 13.2, 12.1, 11.8, 12.56]

def filter(k):
less = []
more = []
for i in k:
if i <= 12:
less.append(i)
else:
more.append(i)
return [less, more]

filter(mud_wt)

[[9.4, 10.5, 11.8], [14.1, 13.2, 12.1, 12.56]]

/
2/2/2021 Python for O&G Lecture 36, 37, 38, 39, 40: Lists Part 2 - Colaboratory

You might also like