# itertools模块
本模块实现了一系列迭代器,标准化了一个快速、高效利用内存的核心工具集,这些工具本身或组合在提高代码性能上很有用。
## 无穷迭代器
| 迭代器 | 实参 | 结果 | 示例 |
| ---------- | -------------- | ------------------------------------- | --------------------------------------- |
| `count()` | start [, step] | start, start+step, start+2*step, ... | count(10) --> 10 11 12 13 14 ... |
| `cycle()` | p | p0, p1, ... plast, p0, p1, ... | `cycle('ABCD') --> A B C D A B C D ...` |
| `repeat()` | elem [,n] | elem, elem, elem, ... 重复无限次或n次 | `repeat(10, 3) --> 10 10 10` |
## 根据最短输入序列长度停止的迭代器
| 迭代器 | 实参 | 结果 | 示例 |
| :----------------------------------------------------------- | :-------------------------- | :----------------------------------------------- | :--------------------------------------------------------- |
| [`accumulate()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.accumulate) | p [,func] | p0, p0+p1, p0+p1+p2, ... | `accumulate([1,2,3,4,5]) --> 1 3 6 10 15` |
| [`chain()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.chain) | p, q, ... | p0, p1, ... plast, q0, q1, ... | `chain('ABC', 'DEF') --> A B C D E F` |
| [`chain.from_iterable()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.chain.from_iterable) | iterable -- 可迭代对象 | p0, p1, ... plast, q0, q1, ... | `chain.from_iterable(['ABC', 'DEF']) --> A B C D E F` |
| [`compress()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.compress) | data, selectors | (d[0] if s[0]), (d[1] if s[1]), ... | `compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F` |
| [`dropwhile()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.dropwhile) | pred, seq | seq[n], seq[n+1], ... 从pred首次真值测试失败开始 | `dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1` |
| [`filterfalse()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.filterfalse) | pred, seq | seq中pred(x)为假值的元素,x是seq中的元素。 | `filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8` |
| [`groupby()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.groupby) | iterable[, key] | 根据key(v)值分组的迭代器 | |
| [`islice()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.islice) | seq, [start,] stop [, step] | seq[start:stop:step]中的元素 | `islice('ABCDEFG', 2, None) --> C D E F G` |
| [`pairwise()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.pairwise) | iterable -- 可迭代对象 | (p[0], p[1]), (p[1], p[2]) | `pairwise('ABCDEFG') --> AB BC CD DE EF FG` |
| [`starmap()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.starmap) | func, seq | func(*seq[0]), func(*seq[1]), ... | `starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000` |
| [`takewhile()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.takewhile) | pred, seq | seq[0], seq[1], ..., 直到pred真值测试失败 | `takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4` |
| [`tee()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.tee) | it, n | it1, it2, ... itn 将一个迭代器拆分为n个迭代器 | |
| [`zip_longest()`](https://docs.python.org/zh-cn/3/library/itertools.html?highlight=itertools#itertools.zip_longest) | p, q, ... | (p[0], q[0]), (p[1], q[1]), ... | `zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-` |
## 排列组合迭代器
| 迭代器 | 实参 | 结果 |
| :-------------------------------- | :------------------- | :------------------------------------ |
| `product()` | p, q, ... [repeat=1] | 笛卡尔积,相当于嵌套的for循环 |
| `permutations()` | p[, r] | 长度r元组,所有可能的排列,无重复元素 |
| `combinations()` | p, r | 长度r元组,有序,无重复元素 |
| `combinations_with_replacement()` | p, r | 长度r元组,有序,元素可重复 |
| 例子 | 结果 |
| :----------------------------------------- | :------------------------------------------------ |
| `product('ABCD', repeat=2)` | `AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD` |
| `permutations('ABCD', 2)` | `AB AC AD BA BC BD CA CB CD DA DB DC` |
| `combinations('ABCD', 2)` | `AB AC AD BC BD CD` |
| `combinations_with_replacement('ABCD', 2)` | `AA AB AC AD BB BC BD CC CD DD` |