-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_itertools.py
More file actions
44 lines (36 loc) · 1.11 KB
/
Copy path7_itertools.py
File metadata and controls
44 lines (36 loc) · 1.11 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
from itertools import product, permutations, \
combinations, combinations_with_replacement, accumulate, groupby, count, cycle
import operator
set1 = 1, 2
set2 = 3, 4
print(list(product(set1, set2)))
print('Permutations')
set3 = 1, 2, 3, 4, 5, 6
print(list(permutations(set3)))
print(list(permutations(set3, 2)))
print('\n')
print('Combinations')
print(list(combinations(set3, 2)))
print(list(combinations_with_replacement(set3, 2)))
print('\n')
print('Accumulation')
print(list(accumulate(set3)))
print(list(accumulate(set3, func=operator.mul)))
print('\n')
print('Group by')
for k, v in groupby(set3, key=lambda x: x < 3):
print(f'{k}\t\t{list(v)}')
print('\n')
persons = ({'name': 'Tim', 'age': 25}, {'name': 'Dan', 'age': 25},
{'name': 'Lisa', 'age': 19}, {'name': 'Claire', 'age': 28},
{'name': 'Rod', 'age': 25}, {'name': 'Hank', 'age': 35},
{'name': 'Britney', 'age': 28})
for k, v in groupby(persons, key=lambda x: x['age'] ):
print(f'{k}\t\t{list(v)}')
print('\n')
# print('Count')
# for n in count(10):
# print(n)
# print('Cycle')
# for i in cycle(set3):
# print(i)