-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitertools
More file actions
executable file
·60 lines (49 loc) · 1.96 KB
/
Copy pathitertools
File metadata and controls
executable file
·60 lines (49 loc) · 1.96 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
#!/usr/bin/env python3
import itertools
a = (1, 2, 3, 4)
b = ("a", "b", "c")
# Stop when b is exhausted (similar to python2's itertools.izip)
res = []
for i in zip(a, b):
res.append(i)
assert(res == [(1, 'a'), (2, 'b'), (3, 'c')])
res = []
# Fill in short lists with NULL (in python3, this is izip_longest)
for i in itertools.zip_longest(a, b):
res.append(i)
assert(res == [(1, 'a'), (2, 'b'), (3, 'c'), (4, None)])
res = []
for i, (a, b) in enumerate(zip(a, b)):
res.append((i, a, b))
a = [1, 2, 3, 4, 5, 6]
b = [ x for x in a if x % 2]
# same as using filter, but filter gives a generator
b = list(filter(lambda x: x%2, a))
"""
NAME
itertools - Functional tools for creating and using iterators.
DESCRIPTION
Infinite iterators:
count(start=0, step=1) --> start, start+step, start+2*step, ...
cycle(p) --> p0, p1, ... plast, p0, p1, ...
repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times
Iterators terminating on the shortest input sequence:
accumulate(p[, func]) --> p0, p0+p1, p0+p1+p2
chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ...
chain.from_iterable([p, q, ...]) --> p0, p1, ... plast, q0, q1, ...
compress(data, selectors) --> (d[0] if s[0]), (d[1] if s[1]), ...
dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred fails
groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)
filterfalse(pred, seq) --> elements of seq where pred(elem) is False
islice(seq, [start,] stop [, step]) --> elements from
seq[start:stop:step]
starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...
tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n
takewhile(pred, seq) --> seq[0], seq[1], until pred fails
zip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ...
Combinatoric generators:
product(p, q, ... [repeat=1]) --> cartesian product
permutations(p[, r])
combinations(p, r)
combinations_with_replacement(p, r)
"""