1. 全部
- count(start=0, step=1)
- repeat(elem [,n])
- accumulate(p[, func])
- chain(p, q, …)
- chain.from_iterable([p, q, …])
- compress(data, selectors)
- dropwhile(pred, seq)
- groupby(iterable[, keyfunc])
- filterfalse(pred, seq)
- islice(seq, [start,] stop [, step])
- starmap(fun, seq)
- tee(it, n=2)
- takewhile(pred, seq)
- zip_longest(p, q, …)
- product(p, q, … [repeat=1])
- permutations(p[, r])
- combinations(p, r)
- combinations_with_replacement(p, r)
本节主要介绍1-7,8-18见下一节内容
2. 详解
"""
@python version: ??
@author: XiangguoSun
@contact: sunxiangguodut@qq.com
@site: http://blog.csdn.net/github_36326955
@software: PyCharm
@file: suggest4.py
@time: 5/2/2017 5:04 PM
"""
import itertools
for x in itertools.count(start=0, step=1):
print(x, end=',')
for x in itertools.cycle('abcd '):
print(x, end='')
for x in itertools.repeat('abc',times=10):
print(x, end=',')
for x in itertools.accumulate("abc"):
print(x, end="|")
def binary_fun(x, y):
return x+y
for x in itertools.accumulate("abc",func=binary_fun):
print(x, end="|")
def reverse(x, y):
return y+x
for x in itertools.accumulate("abc",func=reverse):
print(x, end="|")
for x in itertools.chain("abc", "def", "ghi"):
print(x, end='')
for x in itertools.chain.from_iterable(["abc", [1, 2, 3], ('have', 'has')]):
print(x, end="|")
for x in itertools.compress(['a', 'b', 'c'], [1, 0, 1]):
print(x, end=',')
"""
output:
a,c,
"""
def pred(x):
print('Testing:', x)
return x < 1
for x in itertools.dropwhile(pred, [-1, 0, 1, 2, 3, 4, 1, -2]):
print('Yielding:', x)
"""
output:
Testing: -1
Testing: 0
Testing: 1
Yielding: 1
Yielding: 2
Yielding: 3
Yielding: 4
Yielding: 1
Yielding: -2
"""