itertools是个非常有用的处理iterables的module, 关键是非常的高效(of course using generators!)。 这里简单罗列几个常见的用法,详细的用法可以参见https://docs.python.org/3/library/itertools.html#itertools.permutations
import itertools
# 3 infinite iterators
counter = itertools.count(10,2) # 2 as step, 1 as default like (10,11,12,13,14....)
for p in counter:
if p < 20:
print(p)
else:
break
cs = itertools.cycle(["chalmers","kth","UU"])
n = 0
for c in cs:
if n<6:
print(c)
n = n+1
else:
break
ns = itertools.repeat('A', 5)
for n in ns:
print(n)
# takewhile(predicate, iterable)
# This iterator is opposite of dropwhile(), it prints the values till the function returns false for 1st time.
xs = itertools.takewhile(lambda x: (x< 6), [1,2,3,4,5,6,7,8,9])
for x in xs:
print("using takingwhile:", x)
# groupby
gb = itertools.groupby(["abc","abc","cth","cth","cth","kth","KTH"], lambda c: c.upper())
for x,group in gb:
print(x,":", list(group))
# permutations and chain:
for i in itertools.permutations(["3435","magic","!","@"]): # 所有的组合可能
s = ""
print(i)
for z in itertools.chain(i):
s = s+z
print("possible combinations:",s)
输出:
10
12
14
16
18
chalmers
kth
UU
chalmers
kth
UU
A
A
A
A
A
using takingwhile: 1
using takingwhile: 2
using takingwhile: 3
using takingwhile: 4
using takingwhile: 5
ABC : ['abc', 'abc']
CTH : ['cth', 'cth', 'cth']
KTH : ['kth', 'KTH']
('3435', 'magic', '!', '@')
possible combinations: 3435magic!@
('3435', 'magic', '@', '!')
possible combinations: 3435magic@!
('3435', '!', 'magic', '@')
possible combinations: 3435!magic@
('3435', '!', '@', 'magic')
possible combinations: 3435!@magic
('3435', '@', 'magic', '!')
possible combinations: 3435@magic!
('3435', '@', '!', 'magic')
possible combinations: 3435@!magic
('magic', '3435', '!', '@')
possible combinations: magic3435!@
('magic', '3435', '@', '!')
possible combinations: magic3435@!
('magic', '!', '3435', '@')
possible combinations: magic!3435@
('magic', '!', '@', '3435')
possible combinations: magic!@3435
('magic', '@', '3435', '!')
possible combinations: magic@3435!
('magic', '@', '!', '3435')
possible combinations: magic@!3435
('!', '3435', 'magic', '@')
possible combinations: !3435magic@
('!', '3435', '@', 'magic')
possible combinations: !3435@magic
('!', 'magic', '3435', '@')
possible combinations: !magic3435@
('!', 'magic', '@', '3435')
possible combinations: !magic@3435
('!', '@', '3435', 'magic')
possible combinations: !@3435magic
('!', '@', 'magic', '3435')
possible combinations: !@magic3435
('@', '3435', 'magic', '!')
possible combinations: @3435magic!
('@', '3435', '!', 'magic')
possible combinations: @3435!magic
('@', 'magic', '3435', '!')
possible combinations: @magic3435!
('@', 'magic', '!', '3435')
possible combinations: @magic!3435
('@', '!', '3435', 'magic')
possible combinations: @!3435magic
('@', '!', 'magic', '3435')
possible combinations: @!magic3435