Python标准库(3.x): itertools库扫盲

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Harrytsz/article/details/85017165

Python标准库(3.x): itertools库扫盲

itertools functions
accumulate() compress() groupby() starmap()
chain() count() islice() takewhile()
chain.from_iterable() cycle() permutations() tee()
combinations() dropwhile() product() zip_longest()
combinations_with_replacement() filterfalse() repeat()  

itertools.accumulate(iterable [, func])

返回一个迭代序列的累加值序列(没有func的情况下)。当指定了func(参数必须为两个)后,将通过func进行累加。

注1: 当没有传入func时,func相当于 operator.add

注2: 返回值为迭代器

>>> data = [1,2,3,4]
>>> a = itertools.accumulate(data)
>>> list(a)
[1, 3, 6, 10]
#[1,2,3,4] --> [1, 1+2, (1+2)+3, ((1+2)+3)+4]

>>> b = itertools.accumulate(data, operator.mul)
>>> list(b)
[1, 2, 6, 24]
#[1,2,3,4] --> [1, 1*2, (1*2)*3, ((1*2)*3)*4]

 itertools.chain(*iterables)

连接多个迭代序列为一个迭代序列,适用于需要连续遍历多个序列场景。

注: 返回值为迭代器

>>> a = [1,2,3,4,5]
>>> b = [6,7,8,9,10]
>>> c = itertools.chain(a,b)
>>> list(c)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

 itertools.chain.from_iterable(iterable)

通过一个迭代序列来创建 itertools.chain 的对象。类似于将迭代序列中的每一个对象作为 itertools.chain 的参数,因此传入的迭代序列中的每一个对象应该也是可迭代的。

注1: 返回值为迭代器

>>> a = itertools.chain.from_iterable(['abc', 'def', 'hjk'])
>>> list(a)
['a', 'b', 'c', 'd', 'e', 'f', 'h', 'j', 'k']
>>>
>>> b = itertools.chain.from_iterable([1,2,3])
>>> list(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

 itertools.combinations(iterable, r)

将迭代序列中的对象进行"不重复的"组合并返回所有组合的元组列表,每个组合的元素个数为r。

注1: 这里的“不重复”是指迭代序列中的对象不会使用多次,但并不代表相同的值不会使用多次。

注2: 返回的组合顺序依赖传入的迭代序列中的顺序。

注3: 返回值为迭代器。

>>> a = itertools.combinations('ABC',2)
>>> list(a)
[('A', 'B'), ('A', 'C'), ('B', 'C')]
>>>
>>> b = itertools.combinations('CBA',2)
>>> list(b)
[('C', 'B'), ('C', 'A'), ('B', 'A')]
>>>
>>> c = itertools.combinations('AAC',2)
>>> list(c)
[('A', 'A'), ('A', 'C'), ('A', 'C')]

itertools.combinations_with_replacement(iterable, r)

将迭代序列中的对象进行"可重复的"组合并返回所有组合的元组列表,每个组合的元素个数为r。

注1: 与 itertools.combinations 的唯一区别就是元素可以重复使用。

注2: 返回的组合顺序依赖传入的迭代序列中的顺序。

注3: 返回值为迭代器

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

 itertools.compress(data, selectors)

对 data 中的数据进行过滤,只保留 selectors 中对应位置为 True 的对象。data和selectors的序列长度可以不等,其中任意一个迭代终结,整个迭代即终结。

注1: 返回值为迭代器

>>> a = itertools.compress('ABCDE', [1,1,0,0,0])
>>> list(a)
['A', 'B']
>>>
>>> b = itertools.compress('ABCDE', [1,1])
>>> list(b)
['A', 'B']
>>>
>>> c = itertools.compress('ABC', [1,1,0,0,1])
>>> list(c)
['A', 'B']

 itertools.count(start=0, step=1)

生成一个计数迭代器,可以指定起始点和步进,但是没有终点,可以一直迭代下去。一般需要配合其他的迭代器一起使用,例如作为map(), zip()的参数等。

>>> a = itertools.count(start=1, step=2)
>>> next(a)
>>> next(a)
>>> next(a)
>>> next(a)
>>> next(a)
>>> next(a)
>>> next(a)
>>> 
>>> b = itertools.count(start=100, step=1)
>>> list(zip(b, 'ABCDE'))
[(100, 'A'), (101, 'B'), (102, 'C'), (103, 'D'), (104, 'E')]

 itertools.cycle(iterable)

生成一个循环迭代器,循环遍历传入迭代器中的对象,没有终结。一般需要配合其他迭代器一起使用,例如map(), zip() 的参数等

>>> a = itertools.cycle('ABC')
>>> next(a)
'A'
>>> next(a)
'B'
>>> next(a)
'C'
>>> next(a)
'A'
>>> next(a)
'B'
>>> next(a)
'C'
>>> next(a)
'A'
>>> 
>>> b = itertools.cycle(range(1,4))
>>> list(zip('ABCDEFG', b))
[('A', 1), ('B', 2), ('C', 3), ('D', 1), ('E', 2), ('F', 3), ('G', 1)]

 itertools.dropwhile(predicate, iterable)

对迭代器中的对象按照 predicate 进行断言,丢弃第一个断言为False之前的所有对象。也可以理解为从第一个断言为False的对象开始输出。

注1: 当出现第一个断言为False的对象后,之后的对象不再进行断言。

注2: predicate 代表的函数只能有一个参数。

注3: 返回值为迭代器

>>> a = itertools.dropwhile(lambda x: x<5, [3,4,5,6,5,4,3])
>>> list(a)
[5, 6, 5, 4, 3]

 itertools.filterfalse(predicate, iterable)

过滤掉迭代器中按照 predicate 断言为 True 的对象。如果 predicate 传入None, 则过滤掉值为 True 的对象。

注1: 返回值为迭代器

>>> a = itertools.filterfalse(lambda x: x%2==0, range(10))
>>> list(a)
[1, 3, 5, 7, 9]
>>> 
>>> b = itertools.filterfalse(None, [1,0,1,0,1,0])
>>> list(b)
[0, 0, 0]

 itertools.groupby(iterable, key=None)

对迭代序列中的对象按照key进行分组,如果key为None则按照对象本身的值进行分组。

注1: 如果迭代序列中key值相等的对象中间间隔了其他的key值,则不会分在一个组。

注2: 返回值为一个迭代器且返回的是一个有两个元素的元组,第一个元素为key值,第二个元素为分组对象的迭代器.

>>> data = ['abc-0', 'def-0', 'xyz-1', 'tty-1', 'kkk-2']
>>> a = itertools.groupby(data, lambda x:x[-1])
>>> [(k, list(g)) for k, g in a]
[('0', ['abc-0', 'def-0']), ('1', ['xyz-1', 'tty-1']), ('2', ['kkk-2'])]
>>> 
>>> 
>>> b = itertools.groupby('AAABBBCC')
>>> [(k, list(g)) for k, g in b]
[('A', ['A', 'A', 'A']), ('B', ['B', 'B', 'B']), ('C', ['C', 'C'])]

 itertools.islice(iterable, stopitertools.islice(iterable, start, stop [, step])

对迭代序列进行分片,类似 slice(),但是本函数中 start, stop, step 都不能为负数。

参数 start 如果为 None, 则 start = 0

参数 stop 如果为 None, 则迭代到最后一个

参数 step 如果为 None, 则 step = 1

注1: 返回值为一个迭代器

>>> data = 'ABCDEFG'
>>> a = itertools.islice(data, 3)
>>> list(a)
['A', 'B', 'C']
>>> 
>>> b = itertools.islice(data, 1, 5, 2)
>>> list(b)
['B', 'D']
>>> 
>>> c = itertools.islice(data, None, 3)
>>> list(c)
['A', 'B', 'C']
>>> 
>>> d = itertools.islice(data, 3, None)
>>> list(d)
['D', 'E', 'F', 'G']

itertools.permutations(iterable, r=None)

将迭代序列中的对象进行"不重复的"排列组合并返回所有组合的元组列表,每个组合的元素个数为r。

如果r为None,则长度为迭代序列的长度。

注1: 这里的“不重复”是指迭代序列中的对象不会使用多次,但并不代表相同的值不会使用多次。

注2: 返回的组合顺序依赖传入的迭代序列中的顺序。

注3: 返回值为迭代器。

>>> a = itertools.permutations('ABC', 2)
>>> list(a)
[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
>>> 
>>> b = itertools.permutations('ABC')
>>> list(b)
[('A', 'B', 'C'), ('A', 'C', 'B'), ('B', 'A', 'C'), ('B', 'C', 'A'), ('C', 'A', 'B'), ('C', 'B', 'A')]

itertools.product(*iterables, repeat=1)

返回多个迭代序列的笛卡尔乘积,repeat值相当于把传入的迭代器参数重复的次数。

注1: 返回值是一个迭代器

>>> a = itertools.product('ABCD', 'xy')
>>> list(a)
[('A', 'x'), ('A', 'y'), ('B', 'x'), ('B', 'y'), ('C', 'x'), ('C', 'y'), ('D', 'x'), ('D', 'y')]
>>> 
>>> b = itertools.product(range(2), repeat=3)
>>> list(b)
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
# 相当于 itertools.product(range(2), range(2), range(2))

itertools.repeat(object [, times])

返回一个迭代器,重复传入的对象。重复的次数为 times 。如果没有传入times参数,则无限重复。

>>> a = itertools.repeat('hello', 3)
>>> list(a)
['hello', 'hello', 'hello']
>>> 
>>> b = itertools.repeat('test')
>>> list(map(lambda x, y: x + y, b, 'ABCD'))
['testA', 'testB', 'testC', 'testD']

itertools.starmap(function, iterable)

和 map() 类似。但是这里 function 的参数封装在迭代器中的每一个对象中。

注1: 迭代器中的每一个对象也必须是可迭代的,哪怕函数只有一个参数。

>>> a = itertools.starmap(lambda x,y: x**y, [(2,1), (2,2), (2,3)])
>>> list(a)
[2, 4, 8]
>>> 
>>> b = itertools.starmap(lambda x: x*x, [(1,),(2,),(3,)])
>>> list(b)
[1, 4, 9]

itertools.takewhile(predicate, iterable)

与 dropwhile() 相反,对迭代器中的对象按照 predicate 进行断言,输出第一个断言为False之前的所有对象。

注1: 当出现第一个断言为False的对象后,迭代即终止。

注2: predicate 代表的函数只能有一个参数。

注3: 返回值为迭代器

>>> a = itertools.takewhile(lambda x: x<5, [3,4,5,6,5,4,3])
>>> list(a)
[3, 4]

itertools.tee(iterable, n=2)

将一个迭代器复制n次,返回一个有n个迭代器的元组。n默认为2

>>> a = itertools.tee('ABC')
>>> [list(x) for x in a]
[['A', 'B', 'C'], ['A', 'B', 'C']]
>>> 
>>> b = itertools.tee(range(5), 3)
>>> [list(x) for x in b]
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]

itertools.zip_longest(*iterables, fillvalue=None)

类似于 zip()。但是这里按照最长的迭代序列进行打包,缺少的元素用 fillvalue 的值进行填充。

注1: fillvalue 默认为None, 并且如果是None,填充的就是None

>>> a = itertools.zip_longest('ABC', 'xy', fillvalue='*')
>>> list(a)
[('A', 'x'), ('B', 'y'), ('C', '*')]
>>>
>>> b = itertools.zip_longest('ABC', 'xy')
>>> list(b)
[('A', 'x'), ('B', 'y'), ('C', None)]

猜你喜欢

转载自blog.csdn.net/Harrytsz/article/details/85017165