首页 > 技术文章 > Python学习笔记:itertools迭代器

hider 原文

致力于将 Python 代码写得更加 Pythonic

一来更符合规范且容易阅读,二来一般 Pythonic 的代码在执行上也更有效率。

一、itertools库介绍

迭代器(生成器)在 Python 中是一种很常用、也很好用的数据结构,比起列表 list 来说,迭代器最大的优势就是延迟计算、按需使用,从而提高开发体验和运行效率,以至于在 Python 3mapfilter等操作返回的不再是列表而是迭代器。

日常常用迭代器:range

itertools 中的函数大多是返回各种迭代器对象,因为是系统库,运行效率更高。

二、实操

1.itertools.accumulate

实现累加

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

2.itertools.chain

连接多个列表或者迭代器

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

3.itertools.combinations

求列表或生成器中指定数目的元素不重复的所有组合(举例:C5取3)

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

4.itertools.combinations_with_replacement

可以重复元素的组合(举例:A5取3 -- 貌似也不太恰当)

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

5.itertools.compress

按照真值表筛选元素

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

6.itertools.islice

对迭代器进行切片

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

7.itertools.count

一个计数器,可以指定起始位置和步长

# 记得切片 不可以直接 print
import itertools
x = itertools.count(start=10, step=-1)
print(list(itertools.islice(x, 0, 10, 1)))
# [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

8.itertools.cycle

循环指定的列表和迭代器

# 记得切片 不可以直接 print
import itertools
x = itertools.cycle('ABC')
print(list(itertools.islice(x, 0, 12, 1)))
# ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C']

9.itertools.dropwhile

按照真值函数丢弃列表和迭代器前面的元素

import itertools
x = itertools.dropwhile(lambda x: x < 5, range(20))
print(list(x))
# [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

10.itertools.filterfalse

保留不满足条件的元素

import itertools
x = itertools.filterfalse(lambda x: x < 5, (1,5,2,6,9,4))
print(list(x))
# [5, 6, 9]

11.itertools.groupby

按照分组函数的值对元素进行分组

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

12.itertools.permutations

产生指定数目元素的所有排列(与顺序有关)

import itertools
x = itertools.permutations(range(4), 3) # 4*3*2 
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)]

13.itertools.product

产生多个列表和迭代器的“积”(组合)

import itertools
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)]

14.itertools.repeat

生成一个拥有指定数目元素的迭代器

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

15.itertools.starmap

类似map

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

16.itertools.takewhile

与 dropwhile 相反,保留满足条件的元素

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

17.itertools.tee

生成指定数目的迭代器

import itertools
x = itertools.tee(range(10), 2)
print(list(x))
# [<itertools._tee object at 0x00000226629FB240>, <itertools._tee object at 0x00000226629FBA40>]
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]

18.itertools.zip_longest

类似于 zip,不过以较长的列表和迭代器的长度为准

import itertools
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)]

熟练最重要!!!

多实操!!!

参考链接:python itertools库详解(让你的代码更pythonic)

参考链接:[这段代码很Pythonic]相见恨晚的itertools库

参考链接:一个零差评的 Python 内置库

推荐阅读