-
Notifications
You must be signed in to change notification settings - Fork 0
/
itertools
151 lines (71 loc) · 2.64 KB
/
itertools
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# ****** Infinite Iterators******** (Part 1)
# from itertools import count
# def count_example(start, step):
# counter = count(start, step)
# for c in counter:
# print(c)
# if c == 100:
# break
# count_example(10, 5)
# ******* Infinite Iterators******** (Part 1)
# from itertools import repeat
# def repeat_example(element, max_repeats):
# repeater = repeat(element, max_repeats)
# for val in repeater:
# print(val)
# repeat_example("hello", 10)
# *******Infinite Iterators************ (Part 1)
# from itertools import cycle
# def cycle_example(elements):
# i = 0
# cycler = cycle(elements)
# while i < 100:
# print(next(cycler), end=" ")
# i += 1
# cycle_example("ABCDEF")
# *********Terminating Iterators********* (Part 2)
# from itertools import accumulate
# def accumulate_example(elements):
# running_sum = accumulate(elements)
# print(list(running_sum))
# accumulate_example([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# *********Terminating Iterators********* (Part 2)
# from itertools import chain (A)
# def chain_example(elements1, elements2):
# chained = chain(elements1, elements2)
# print(list(chained))
# chain_example("ABC", "DEF")
# from itertools import chain (B)
# def chain_from_iterable_example(iterable): #Flattens the list
# chained = chain.from_iterable(iterable)
# print(list(chained))
# chain_from_iterable_example([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# *********Terminating Iterators********* (Part 2)
# from itertools import compress
# def compress_example(data, selectors):
# compressed = compress(data, selectors)
# print(list(compressed))
# compress_example([["a", "b", "c"], [1, 2, 3], [1, 0, 1], [True, False, True]])
# from itertools import pairwise
# def pairwise_example(iterable):
# paired = pairwise(iterable)
# print(list(paired))
# pairwise_example([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# from itertools import pairwise
# *********Combinatoric Iterators********* (Part 3)
# from itertools import product
# def product_example(iterable1, iterable2):
# result = product(iterable1, iterable2)
# print(list(result))
# product_example([1, 2, 3], ["a", "b", "c"])
# from itertools import permutations
# def permutations_example(iterable, size):
# result = permutations(iterable, size)
# print(list(result))
# permutations_example("ABCD", 2)
from itertools import combinations
def combinations_example(iterable, size):
result = combinations(iterable, size)
print(list(result))
combinations_example("ABCD", 2)
# https://docs.python.org/3/library/itertools.html