Skip to content

Commit ec4cc15

Browse files
committed
Add itertools module examples
1 parent 2646707 commit ec4cc15

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

useful_samples/itertools_module.py

+45-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,52 @@
1+
'''
2+
Itertools module : returns iterators on
3+
- combinations : Associate 2 items of the list
4+
- permutations : Associate 2 items of the list and their permutations
5+
- combinations_with_replacement : Associate 2 items of the list and the doubled items
6+
- product : Associate 2 items of the list, their permutations and the doubled items
7+
'''
18
from itertools import combinations, permutations, combinations_with_replacement, product
29

3-
# Combinations : Associate 2 items of the list
4-
print( f"{[x+y for (x,y) in combinations('ABCD', 2)] = }" )
5-
# ['AB', 'AC', 'AD', 'BC', 'BD', 'CD']
10+
obj_list = [1,2,3,4]
11+
print("\nworks on lists :", obj_list)
12+
print( f"{[i for i in combinations(obj_list, 2)] = }" )
13+
# [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
14+
print( f"{[i for i in permutations(obj_list, 2)] = }" )
15+
# [(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
16+
print( f"{[i for i in combinations_with_replacement(obj_list,2)] = }" )
17+
# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
18+
print( f"{[i for i in product(obj_list, repeat=2)] = }" )
19+
# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]
620

7-
# Permutations : Associate 2 items of the list and their permutations
8-
print( f"{[x+y for (x,y) in permutations('ABCD', 2)] = }" )
9-
# ['AB', 'AC', 'AD', 'BA', 'BC', 'BD', 'CA', 'CB', 'CD', 'DA', 'DB', 'DC']
21+
obj_list = (1,2,3,4)
22+
print("\nworks on tuples :", obj_list) # Same results than list
23+
print( f"{[i for i in combinations(obj_list, 2)] = }" )
24+
# [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
25+
print( f"{[i for i in permutations(obj_list, 2)] = }" )
26+
# [(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
27+
print( f"{[i for i in combinations_with_replacement(obj_list,2)] = }" )
28+
# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)]
29+
print( f"{[i for i in product(obj_list, repeat=2)] = }" )
30+
# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]
1031

11-
# Combinations : Associate 2 items of the list and the doubled items
12-
print( f"{[x+y for (x,y) in combinations_with_replacement('ABCD',2)] = }" )
32+
obj_list = 'ABCD'
33+
print("\nworks on string :", obj_list)
34+
print( f"{[x+y for (x,y) in combinations(obj_list, 2)] = }" )
35+
# ['AB', 'AC', 'AD', 'BC', 'BD', 'CD']
36+
print( f"{[x+y for (x,y) in permutations(obj_list, 2)] = }" )
37+
# ['AB', 'AC', 'AD', 'BA', 'BC', 'BD', 'CA', 'CB', 'CD', 'DA', 'DB', 'DC']
38+
print( f"{[x+y for (x,y) in combinations_with_replacement(obj_list,2)] = }" )
1339
# ['AA', 'AB', 'AC', 'AD', 'BB', 'BC', 'BD', 'CC', 'CD', 'DD']
14-
15-
# Product : Associate 2 items of the list, their permutations and the doubled items
16-
print( f"{[x+y for (x,y) in product('ABCD', repeat=2)] = }" )
40+
print( f"{[x+y for (x,y) in product(obj_list, repeat=2)] = }" )
1741
# ['AA', 'AB', 'AC', 'AD', 'BA', 'BB', 'BC', 'BD', 'CA', 'CB', 'CC', 'CD', 'DA', 'DB', 'DC', 'DD']
1842

19-
20-
43+
obj_list = 'TTST'
44+
print("\nand doesn't take care of duplicates :", obj_list)
45+
print( f"{[x+y for (x,y) in combinations(obj_list, 2)] = }" )
46+
# ['AB', 'AC', 'AD', 'BC', 'BD', 'CD']
47+
print( f"{[x+y for (x,y) in permutations(obj_list, 2)] = }" )
48+
# ['AB', 'AC', 'AD', 'BA', 'BC', 'BD', 'CA', 'CB', 'CD', 'DA', 'DB', 'DC']
49+
print( f"{[x+y for (x,y) in combinations_with_replacement(obj_list,2)] = }" )
50+
# ['AA', 'AB', 'AC', 'AD', 'BB', 'BC', 'BD', 'CC', 'CD', 'DD']
51+
print( f"{[x+y for (x,y) in product(obj_list, repeat=2)] = }" )
52+
# ['AA', 'AB', 'AC', 'AD', 'BA', 'BB', 'BC', 'BD', 'CA', 'CB', 'CC', 'CD', 'DA', 'DB', 'DC', 'DD']

0 commit comments

Comments
 (0)