Python模块学习5-itertools
模块说明
用STL写c++代码,其中好多集成的数据结构都有内置迭代器,用迭代器的好处既可以提高程序执行效率,又可以减少程序对系统内存的占用。而python的itertools模块实现了很多数据结构的生产,返回的不是传统变量对象,而是迭代器,这等实用模块肯定必学的。但是,找了半天源码都没找到,应该使用c写的模块,可以参见github的cpython项目下的Modules/itertoolsmodule.c文件。
没python源码读只能看帮助文档和互联网了[哭]
itertools有以下对象:count、chain、combinations、combinations_with_replacement、compress、cycle、dropwhile、groupby、ifilter、ifilterfalse、imap、islice、izip、izip_longest、permutations、product、repeat、starmap、takewhile
每个对象都有一个next()方法,用于单步迭代。
count
count(start=0, step=1)
: 用于产生一个无限数值的迭代器,start为开始的数值,step为步长。
import itertools
for c in itertools.count(1, 5):
print c
if c > 20: break
# 1 6 11 16 21
chain
chain(\*iterables)
: 拼接多个可迭代对象并返回。其中方法chain.from_iterable(iterable)
用于将可迭代对象转化成迭代器并返回。
import itertools
for c in itertools.chain(['a', 'bd'], 'ha'):
print c
# a bd h a
for c in itertools.chain.from_iterable(['a', 'bc']):
print c
# a b c
print list(itertools.chain.from_iterable([[1, 2], [3, 4], [5, 6]]))
# [1, 2, 3, 4, 5, 6]
cycle
cycle(iterable)
: 产生无限重复传入的迭代对象的属性的迭代器。传入的参数为可迭代对象。
import itertools
i = 0
for c in itertools.cycle(['a', 'bd', 'c']):
print c
i += 1
if i > 4: break
# a bd c a bd
repeat
repeat(object [,times])
: 产生第一个参数对象无限重复的迭代器,第二参数可选为重复的次数。
import itertools
for c in itertools.repeat(['a', 'bd', 'c'], 2):
print c
# ['a', 'bd', 'c']
# ['a', 'bd', 'c']
compress
compress(data, selectors)
: 将data和selectors相对应,留下所有对应为True的值的迭代器。
import itertools
for c in itertools.compress('abd', [1, 0, 1]):
print c
# a d
dropwhile
dropwhile(predicate, iterable)
: 参数predicate为函数,iterable为可迭代对象,当predicate(item)为True是则抛弃,返回之后所有项的迭代器。
import itertools
print list(itertools.dropwhile(lambda x: x == 'a', 'aaabad'))
# ['b', 'a', 'd']
groupby
groupby(iterable[, keyfunc])
: 对可迭代对象进行值分组,返回类似key(value)类型的(key, sub-iterator)迭代器, 其中sub-iterator也为迭代器。keyfunc为分组函数,分组以keyfunc处理后为准。
import itertools
for x, y in itertools.groupby('aaaabbbccaacbb'):
print x, list(y)
# a ['a', 'a', 'a', 'a']
# b ['b', 'b', 'b']
# c ['c', 'c']
# a ['a', 'a']
# c ['c']
# b ['b', 'b']
for x, y in itertools.groupby([2, 4, 3, 5, 7, 8], lambda x: x%2)
print x, list(y)
# 0 [2, 4]
# 1 [3, 5, 7]
# 0 [8]
ifilter
ifilter(function or None, sequence)
: 返回当function(item)为True的值。
import itertools
print list(itertools.ifilter(lambda x: x%2, [3, 5, 2, 4, 7, 8]))
# [3, 5, 7]
ifilterfalse
ifilterfalse(function or None, sequence)
: ifilter的反操作,返回当function(item)为False的值。
import itertools
print list(itertools.ifilterfalse(lambda x: x%2, [3, 5, 2, 4, 7, 8]))
# [2, 4, 8]
takewhile
takewhile(predicate, iterable)
: 返回当predicate(item)为True的值,一旦出现False则停止后面扫描。
import itertools
print list(itertools.takewhile(lambda x: x%2, [3, 5, 2, 4, 7, 8]))
# [3, 5]
imap
imap(func, *iterables)
: 第一个参数为处理的函数,函数的参数个数与可迭代对象的个数相同,之后传入多个可迭代对象,有效处理个数为多个可迭代对象中值个数最少一个的个数。
import itertools
print list(itertools.imap(lambda x,y,z: x*y*z, [2, 4, 2, 3], [5, 7, 8], [1, 0.5]))
# [10, 14.0]
islice
islice(iterable, [start,] stop [, step])
: 切割可迭代对象并返回对应迭代器,iterable为可迭代对象,start为开始位置默认为0,stop为结束位置,step为步长默认为1。
import itertools
print list(itertools.islice(itertools.count(), 2, 15, 3))
# [2, 5, 8, 11, 14]
izip
izip(iter1 [,iter2 [...]])
: 对多个可迭代对象进行组合并返回它们的迭代器。有效处理个数为多个可迭代对象中值个数最少一个的个数。
import itertools
print list(itertools.izip([2, 4, 2, 3], 'abc', [1, 0.5]))
# [(2, 'a', 1), (4, 'b', 0.5)]
izip_longest
izip_longest(iter1 [,iter2 [...]])
: 对多个可迭代对象进行组合并返回它们的迭代器。有效处理个数为多个可迭代对象中值个数最多一个的个数。
import itertools
print list(itertools.izip_longest([2, 4, 2, 3], 'abc', [1, 0.5]))
# [(2, 'a', 1), (4, 'b', 0.5), (2, 'c', None), (3, None, None)]
permutations
permutations(iterable[, r])
: 返回可迭代对象的每r个排列一次的所有排列的迭代器。
import itertools
print list(itertools.permutations([1, 2], 2))
# [(1, 2), (2, 1)]
print list(itertools.permutations([1, 2, 3], 2))
# [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
print list(itertools.permutations([1, 2, 3], 3))
# [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
combinations
combinations(iterable[, r])
: 返回可迭代对象的每r个组合一次的所有组合的迭代器。
import itertools
print list(itertools.combinations([1, 2], 2))
# [(1, 2)]
print list(itertools.combinations([1, 2, 3], 2))
# [(1, 2), (1, 3), (2, 3)]
print list(itertools.combinations([1, 2, 3], 3))
# [(1, 2, 3)]
combinations_with_replacement
combinations_with_replacement(iterable[, r])
: 返回可迭代对象的每r个组合一次的所有组合的迭代器,允许重复自身。
import itertools
print list(itertools.combinations_with_replacement([1, 2], 2))
# [(1, 1), (1, 2), (2, 2)]
print list(itertools.combinations_with_replacement([1, 2, 3], 2))
# [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]
print list(itertools.combinations_with_replacement([1, 2, 3], 3))
# [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 2), (1, 2, 3), (1, 3, 3), (2, 2, 2), (2, 2, 3), (2, 3, 3), (3, 3, 3)]
product
product(*iterables)
: 返回多个可迭代对象相互组合的笛卡尔积的迭代器,相当于应用多层for循环。
import itertools
print list(itertools.product([1, 2], 'ab', {'x':'xx', 'y':'yy'}))
# [(1, 'a', 'y'), (1, 'a', 'x'), (1, 'b', 'y'), (1, 'b', 'x'), (2, 'a', 'y'), (2, 'a', 'x'), (2, 'b', 'y'), (2, 'b', 'x')]