# 这段代码很Pythonic | 相见恨晚的 itertools 库

itertools库

itertools中的函数大多是返回各种迭代器对象，其中很多函数的作用我们平时要写很多代码才能达到，而在运行效率上反而更低，毕竟人家是系统库。

itertools.accumulate

>>> import itertools >>> x = itertools.accumulate(range(10)) >>> print(list(x)) [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

itertools.chain

>>> x = itertools.chain(range(3), range(4), [3,2,1]) >>> print(list(x)) [0, 1, 2, 0, 1, 2, 3, 3, 2, 1]

itertools.combinations

>>> x = itertools.combinations(range(4), 3) >>> print(list(x)) [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]

itertools.combinations_with_replacement

>>> x = itertools.combinations_with_replacement('ABC', 2) >>> print(list(x)) [('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]

itertools.compress

>>> x = itertools.compress(range(5), (True, False, True, True, False)) >>> print(list(x)) [0, 2, 3]

itertools.count

>>> x = itertools.count(start=20, step=-1) >>> print(list(itertools.islice(x, 0, 10, 1))) [20, 19, 18, 17, 16, 15, 14, 13, 12, 11]

itertools.cycle

>>> x = itertools.cycle('ABC') >>> print(list(itertools.islice(x, 0, 10, 1))) ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']

itertools.dropwhile

>>> x = itertools.dropwhile(lambda e: e < 5, range(10)) >>> print(list(x)) [5, 6, 7, 8, 9]

itertools.filterfalse

>>> x = itertools.filterfalse(lambda e: e < 5, (1, 5, 3, 6, 9, 4)) >>> print(list(x)) [5, 6, 9]

itertools.groupby

>>> x = itertools.groupby(range(10), lambda x: x < 5 or x > 8) >>> for condition, numbers in x: ... print(condition, list(numbers)) True [0, 1, 2, 3, 4] False [5, 6, 7, 8] True [9]

itertools.islice

>>> x = itertools.islice(range(10), 0, 9, 2) >>> print(list(x)) [0, 2, 4, 6, 8]

itertools.permutations

>>> x = itertools.permutations(range(4), 3) >>> print(list(x)) [(0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 3), (0, 3, 1), (0, 3, 2), (1, 0, 2), (1, 0, 3), (1, 2, 0), (1, 2, 3), (1, 3, 0), (1, 3, 2), (2, 0, 1), (2, 0,3), (2, 1, 0), (2, 1, 3), (2, 3, 0), (2, 3, 1), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 2), (3, 2, 0), (3, 2, 1)]

itertools.product

>>> x = itertools.product('ABC', range(3)) >>> >>> print(list(x)) [('A', 0), ('A', 1), ('A', 2), ('B', 0), ('B', 1), ('B', 2), ('C', 0), ('C', 1), ('C', 2)]

itertools.repeat

>>> x = itertools.repeat(0, 5) >>> print(list(x)) [0, 0, 0, 0, 0]

itertools.starmap

>>> x = itertools.starmap(str.islower, 'aBCDefGhI') >>> print(list(x)) [True, False, False, False, True, True, False, True, False]

itertools.takewhile

>>> x = itertools.takewhile(lambda e: e < 5, range(10)) >>> print(list(x)) [0, 1, 2, 3, 4]

itertools.tee

>>> x = itertools.tee(range(10), 2) >>> for letters in x: ... print(list(letters)) ... [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

itertools.zip_longest

>>> x = itertools.zip_longest(range(3), range(5)) >>> y = zip(range(3), range(5)) >>> print(list(x)) [(0, 0), (1, 1), (2, 2), (None, 3), (None, 4)] >>> print(list(y)) [(0, 0), (1, 1), (2, 2)]

705 篇文章58 人订阅

0 条评论

## 相关文章

9310

364100

429110

### 关于.NET参数传递方式的思考

年关将近，整个人已经没有了工作和写作的激情，估计这个时候很多人跟我差不多，该相亲的相亲，该聚会喝酒的聚会喝酒，总之就是没有了干活的心思（我有很多想...

24390

30140

### Python读书笔记（数字型数据）

Python与其它编程语言一样，常见的数字型无法整型（int）和浮点型两种（Float）两种。 整型就是整数，浮点型就是小数。 如果在Python中输入一个数字...

38750

37870

48670

43070

### 面向对象的Java实现

1-1:封装 a.为什么需要封装(封装可以是数据方便维护、增加实用性、方便扩展等等。通过面向对象的思想，模拟现实生活中的事物。) b.什么是封装(封装就是将...

10210