-
Notifications
You must be signed in to change notification settings - Fork 8
/
cheatsheet-itertools.py
50 lines (38 loc) · 1.09 KB
/
cheatsheet-itertools.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
# ___ _ _ _
# |_ _| |_ ___ _ __| |_ ___ ___ | |___
# | || __/ _ \ '__| __/ _ \ / _ \| / __|
# | || || __/ | | || (_) | (_) | \__ \
# |___|\__\___|_| \__\___/ \___/|_|___/
#
# https://docs.python.org/2/library/itertools.html
import itertools
for i in itertools.chain([1, 2, 3], ['a', 'b', 'c']):
print i
for i in itertools.count(5):
print i
if i == 20:
break
j = 0
for i in itertools.cycle([1, 2, 3]):
j += 1
print i
if j == 10:
break
for i in itertools.izip([1, 2, 3], ['a', 'b', 'c']):
print i
for i in itertools.compress('ABCDEF', [1,0,1,0,1,1]):
print i
for i in itertools.product('AB', 'CD'):
print i
print '--product'
for i in itertools.product('ABC', repeat=2):
print i
print '--permutations'
for i in itertools.permutations('ABC', 2):
print i
print '--combinations'
for i in itertools.combinations('ABC', 2):
print i
print '--combinations_with_replacement'
for i in itertools.combinations_with_replacement('ABC', 2):
print i