-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedin_python_itertools.py
More file actions
51 lines (34 loc) · 1.34 KB
/
linkedin_python_itertools.py
File metadata and controls
51 lines (34 loc) · 1.34 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
# Itertools
# itertools is a module that contains a number of iterator building blocks.
# The module standardizes a core set of fast, memory efficient tools that are
# useful by themselves or in combination. Together, they form an iterator
# algebra making it possible to construct specialized tools succinctly and
import itertools
def testFunction(x):
return x < 40
def main():
# TODO: cycle iterator can be used to cycle over a collection
seq1 = ["Joe", "John", "Mike"]
cycle1 = itertools.cycle(seq1)
# print(next(cycle1))
# print(next(cycle1))
# print(next(cycle1))
# print(next(cycle1))
# TODO: use count to create a simple counter
count1 = itertools.count(100,5)
# print(next(count1))
# print(next(count1))
# print(next(count1))
# print(next(count1))
# TODO: accumulate creates an iterator that accumulates values
vals = [10,20,30,40,50,40,30]
acc = itertools.accumulate(vals,max)
# print(list(acc))
# TODO: use chain to connect sequences together
x = itertools.chain("ABCD", "1234")
# print(list(x))
# TODO: dropwhile and takewhile will return values until a certain condition is met that stops them
print(list(itertools.dropwhile(testFunction, vals)))
print(list(itertools.takewhile(testFunction, vals)))
if __name__ == "__main__":
main()