相见恨晚的 Python 内置库:itertools
↓推荐关注↓
>>>
import itertools
>>> x = itertools.accumulate(range(
10))
>>> print(list(x))
[
0,
1,
3,
6,
10,
15,
21,
28,
36,
45]
>>> x = itertools.chain(range(
3), range(
4), [
3,
2,
1])
>>> print(list(x))
[
0,
1,
2,
0,
1,
2,
3,
3,
2,
1]
> x = itertools.combinations(range(
4),
3)
>>> print(list(x))
[(
0,
1,
2), (
0,
1,
3), (
0,
2,
3), (
1,
2,
3)]
>>> x = itertools.combinations_with_replacement(
'ABC',
2)
>>> print(list(x))
[(
'A',
'A'), (
'A',
'B'), (
'A',
'C'), (
'B',
'B'), (
'B',
'C'), (
'C',
'C')]
>>> x = itertools.compress(range(
5), (
True,
False,
True,
True,
False))
>>> print(list(x))
[
0,
2,
3]
>>> 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]
>>> x = itertools.cycle(
'ABC')
>>> print(list(itertools.islice(x,
0,
10,
1)))
[
'A',
'B',
'C',
'A',
'B',
'C',
'A',
'B',
'C',
'A']
>>> x = itertools.dropwhile(
lambda e: e <
5, range(
10))
>>> print(list(x))
[
5,
6,
7,
8,
9]
>>> x = itertools.filterfalse(
lambda e: e <
5, (
1,
5,
3,
6,
9,
4))
>>> print(list(x))
[
5,
6,
9]
>>> x = itertools.groupby(range(
10),
lambda x: x <
5or x >
8)
>>>
for condition, numbers
in x:
... print(condition, list(numbers))
True [
0,
1,
2,
3,
4]
False [
5,
6,
7,
8]
True [
9]
>>> x = itertools.islice(range(
10),
0,
9,
2)
>>> print(list(x))
[
0,
2,
4,
6,
8]
>>> 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)]
>>> 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)]
>>> x = itertools.repeat(
0,
5)
>>> print(list(x))
[
0,
0,
0,
0,
0]
>>> x = itertools.starmap(str.islower,
'aBCDefGhI')
>>> print(list(x))
[
True,
False,
False,
False,
True,
True,
False,
True,
False]
>>> x = itertools.takewhile(
lambda e: e <
5, range(
10))
>>> print(list(x))
[
0,
1,
2,
3,
4]
>>> 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]
>>> 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)]
来源:忆先链接:segmentfault.com/a/1190000008590958
- EOF -
觉得本文对你有帮助?请分享给更多人
推荐关注「Python开发者」,提升Python技能
点赞和在看就是最大的支持❤️
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。