-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitertools.py
More file actions
101 lines (66 loc) · 1.61 KB
/
itertools.py
File metadata and controls
101 lines (66 loc) · 1.61 KB
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
from itertools import count, cycle, repeat, chain, compress, \
dropwhile, islice, starmap, takewhile, tee, groupby, \
product, permutations, combinations, combinations_with_replacement
print count(10, 1)
print cycle('asdf')
print list(repeat(10, 3))
print '='*60
# print accumulate([1,2,3,4,5]) //python3.5
mm = chain([1,2,3,4], (3,4,5,6))
mm = chain({1:2,3:4}, {3:4,5:6})
print list(mm)
print '='*60
mm = chain.from_iterable(['asd', 'bcd'])
print list(mm)
print '='*60
mm = compress('asdcd', [1,0,1,0.1])
print list(mm)
print '='*60
mm = dropwhile(lambda x: x<5, [1,4,6,1])
print list(mm)
print '='*60
# print filterflase(lambda x: x%2, range(10)) //python3.5
mm = islice([1,2,34,4,5,5], 2, 4, 1)
nn = [1,2,34,4,5,5]
print list(mm), nn[2:4:1]
print '='*60
mm = starmap(pow, [[2,3],[3,3]])
nn = map(pow, [2,3], [3,3])
print list(mm), nn
print '='*60
mm = takewhile(lambda x: x<5, [1,4,8,4,1])
print list(mm)
print '='*60
mm = tee([1,2,3,4,5], 3)
for i in mm:
print list(i)
print '='*60
# mm = zip_longest([1,2,3], [4,5,6])
# print list(mm)
def test(m):
if m < 10:
return "low"
elif m < 20:
return "middle"
else:
return "high"
mm = groupby(sorted([1,11,35,14,50,100]), key = test)
for m, n in mm:
print m
print list(n)
print '='*60
mm = product('asdf', 'hjkl', repeat = 1)
print len(list(mm))
print '='*60
mm = permutations('asdf', 3)
# print list(mm)
print len(list(mm))
print '='*60
mm = combinations('asdf', 2)
# print list(mm)
print len(list(mm))
print '='*60
mm = combinations_with_replacement('asdf', 2)
print list(mm)
print len(list(mm))
print '='*60