-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathitertools_tutorial.py
112 lines (90 loc) · 3.13 KB
/
itertools_tutorial.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import itertools
import operator
def example_1():
# itertools.count(start=0, step=1)
gen_1 = itertools.count(1, 2)
print(next(gen_1))
print(next(gen_1))
print(next(gen_1))
print(next(gen_1))
print(next(gen_1))
def example_2():
# it produces a generator that consumes
# another generator and stops when a given predicate evaluates to False .
# So we can combine the two and write this:
gen_2 = itertools.takewhile(lambda n: n < 5, itertools.count(0, 1))
print(list(gen_2))
def my_key(c):
return c.lower() in 'abcde'
def example_3():
print(list(filter(my_key, 'AbXHJsuaiqm')))
print(list(itertools.filterfalse(my_key, 'AbXHJsuaiqm')))
def example_4():
# dropwhile(predicate, it)
# Consumes it skipping items while predicate computes truthy, then
# yields every remaining item (no further checks are made)
print(list(itertools.dropwhile(my_key, 'AbXHJsuaiqm')))
def example_5():
# takewhile(predicate, it)
# Yields items while predicate computes truthy, then stops and
# no further checks are made
print(list(itertools.takewhile(my_key, 'AbXHJsuaiqm')))
def example_6():
# compress(it, selector_it)
# Consumes two iterables in parallel; yields items from it whenever the
# corresponding item in selector_it is truthy
print(list(itertools.compress('abcdefg', (1,0,1,1,0,1))))
def example_7():
# accumulate(p [,func])
# p0, p0+p1, p0+p1+p2
print(list(itertools.accumulate([1,2,3,4,5])))
print(list(itertools.accumulate([9,4,2,0,5,8], min)))
print(list(itertools.accumulate([1,0,9,2,10], max)))
print(list(itertools.accumulate([1,2,3,4,5], operator.mul)))
def example_8():
# enumerate(iterable, start=0)
print(list(enumerate('abcdefg', 0)))
def example_9():
# Yield all items from it1 , then from it2 etc., seamlessly
# chain(it1, ..., itN)
print(list(itertools.chain('abcdef', range(8))))
def example_10():
# zip_longest(it1, ...,itN, fillvalue=None)
print(list(itertools.zip_longest('ab', range(6))))
print(list(itertools.zip_longest('ab', range(6), fillvalue='@')))
def example_11():
print(list(itertools.product('ab', range(3)))) # 3*2 = 6(tuple)
data = [1, 2, 3, 4]
print(list(itertools.product('ab', data))) # 4*2 = 8(tuple)
print(list(itertools.product('abc', repeat=2)))
print(list(itertools.product(range(2), repeat=4)))
def example_12():
cy = itertools.cycle('abc')
for _ in range(10):
print(next(cy))
def example_13():
rp = itertools.repeat('b') # forever. b
for _ in range(10):
print(next(rp))
print(list(itertools.repeat('c', 4))) # repeat 4 times
def example_14():
print(list(itertools.combinations('ABCD', 2)))
print(list(itertools.combinations_with_replacement('ABCD', 2)))
def example_15():
print(list(itertools.permutations('ABCD', 2)))
if __name__ == "__main__":
# example_1()
# example_2()
# example_3()
# example_4()
# example_5()
# example_6()
# example_7()
# example_8()
# example_9()
# example_10()
# example_11()
# example_12()
# example_13()
# example_14()
example_15()