0% found this document useful (0 votes)
19 views

Python For Og Lecture 69 and 70 - Zip Function

The document discusses the zip function in Python. It shows how zip can be used to combine multiple lists into tuples and iterate over them. Examples demonstrate calculating averages and appending values to a new list using zip. The document also shows how to 'unzip' tuples back into separate lists.
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)
19 views

Python For Og Lecture 69 and 70 - Zip Function

The document discusses the zip function in Python. It shows how zip can be used to combine multiple lists into tuples and iterate over them. Examples demonstrate calculating averages and appending values to a new list using zip. The document also shows how to 'unzip' tuples back into separate lists.
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
You are on page 1/ 5

2/3/2021 Python for O&G Lecture 69 and 70: zip 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

a = [1,2,3,4]
b = [5,6,7,8]

# i want output ((1, 5), (2, 6), (3, 7), (4, 8))

c = list(zip(a, b))
list(c)

[(1, 5), (2, 6), (3, 7), (4, 8)]

/
# for loop
2/3/2021 Python for O&G Lecture 69 and 70: zip function - Colaboratory
# for loop
for i in c:
print(i)

(1, 5)
(2, 6)
(3, 7)
(4, 8)

for i, j in c:
print(i+ j)

6
8
10
12

Excercise problem

s_w = [0.1, 0.2, 0.3, 0.4]


s_g = [0.05, 0.09, 0.11, 0.2]
s_o = []

for i, j in zip(s_w, s_g):


s_o.append(1-i-j)

print(s_o)

[0.85, 0.7100000000000001, 0.59, 0.39999999999999997]

list(zip(s_w, s_g))

[(0.1, 0.05), (0.2, 0.09), (0.3, 0.11), (0.4, 0.2)]

unzipping using zip function and * operator


/
2/3/2021 Python for O&G Lecture 69 and 70: zip function - Colaboratory

d = list(zip(s_w, s_g))
print(d)

[(0.1, 0.05), (0.2, 0.09), (0.3, 0.11), (0.4, 0.2)]

list(zip(*d))

[(0.1, 0.2, 0.3, 0.4), (0.05, 0.09, 0.11, 0.2)]

Assignment 18

# we have permeabilities of multiple zones for diff reservoir

k1 = [19, 24, 31, 34, 45]


k2 = [16, 17, 38, 29, 19]
k3 = [24, 24, 40, 37, 15]
k4 = [50, 47, 19, 49, 10]

# using zip function calculate the average (long method)

perm = list(zip(k1, k2, k3, k4))


print(perm)

[(19, 16, 24, 50), (24, 17, 24, 47), (31, 38, 40, 19), (34, 29, 37, 49), (45, 19, 15, 10)]

for i, j, k, l in perm:
print((i+j+k+l)/4)

27.25
28.0
32.0
/
2/3/2021 Python for O&G Lecture 69 and 70: zip function - Colaboratory
37.25
22.25

# let's say I want to do this problem for 9 lists. How would you do that?

perm = list(zip(k1, k2, k3, k4))


print(perm)

[(19, 16, 24, 50), (24, 17, 24, 47), (31, 38, 40, 19), (34, 29, 37, 49), (45, 19, 15, 10)]

for i in perm:
print(sum(i)/len(i))

27.25
28.0
32.0
37.25
22.25

/
2/3/2021 Python for O&G Lecture 69 and 70: zip function - Colaboratory

You might also like