-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip_ops.py
79 lines (66 loc) · 2.34 KB
/
zip_ops.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# The function takes in iterables as arguments and returns an iterator.
# The iterator generates a series of tuples containing elements from each iterable.
numbers = [1, 2, 3] # iterable
letters = ['a', 'b', 'c'] # iterable
zipped = zip(numbers, letters) # iterator
type(zipped)
list(zipped)
next(zipped)
s1 = {2, 3, 1}
s2 = {'b', 'a', 'c'}
list(zip(s1, s2))
integers = [1, 2, 3]
letters = ['a', 'b', 'c']
floats = [4.0, 5.0, 6.0]
zipped = zip(integers, letters, floats) # three input iterables
list(zipped)
l1 = [2, 3, 1]
d1 = {'b', 'a', 'c'}
list(zip(l1, d1)) # iterables mix
# build dict
fields = ['name', 'last_name', 'age', 'job']
values = ['John', 'Doe', '32', 'Python Developer']
a_dict = dict(zip(fields, values))
a_dict
values2 = [['John', 'Doe', '32', 'Python Developer'], ['Bob', 'Roi', '25', 'Java Developer']]
dev_dict = [dict(zip(fields, sublist)) for sublist in values2]
dev_dict
list(map(lambda x: dict(zip(fields, x)), values2))
weather = ['Thunder', 'Sleet', 'Hail', 'Dust', 'Smoke', 'Blowing_Snow', 'Rain', 'Snow', 'Glaze', 'Fog']
dummy = ['0010000000', '0000000001']
list(map(lambda x: dict(zip(weather, x)), dummy))
list(zip(range(5), range(100))) # unequal lenght: shortest rule
from itertools import zip_longest
numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
longest = range(5)
zipped = zip_longest(numbers, letters, longest, fillvalue='?')
list(zipped)
# loop over multiple iterables (list)
letters = ['a', 'b', 'c']
numbers = [0, 1, 2]
for l, n in zip(letters, numbers):
print(f'Letter: {l} - Number: {n}')
# loop over multiple iterables (dict)
dict_one = {'name': 'John', 'last_name': 'Doe', 'job': 'Python Consultant'}
dict_two = {'name': 'Jane', 'last_name': 'Fly', 'job': 'Community Manager'}
for (k1, v1), (k2, v2) in zip(dict_one.items(), dict_two.items()):
print(k1, '->', v1, '|', k2, '->', v2)
# unzip with unpacking operator *
pairs = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
numbers, letters = zip(*pairs)
numbers
letters
# zip and sort
letters = ['b', 'a', 'd', 'c']
numbers = [2, 4, 3, 1]
data1 = sorted(zip(letters, numbers)) # sort by letters
data2 = sorted(zip(numbers, letters)) # sort by numbers
data1
data2
# calculation in pairs
total_sales = [52000.00, 51000.00, 48000.00]
prod_cost = [46800.00, 45900.00, 43200.00]
for sales, costs in zip(total_sales, prod_cost):
profit = sales - costs
print(f'Total profit: {profit}')