Python itertools模块
今天学了简单好玩的模块。itertools模块,简单的说,itertools模块就是产生一个循环器
来看下这个模块的功能都有哪些吧
无穷循环器
count() 从開始的数字一直数下去
count(10) #--> 10 11 12 13 14 15 …
count(10,2) #--> 10 12 14 16 18 20 …
cycle() 不停的将可迭代元素进行循环
cycle(‘abcd’) #--> a b c d a b c d a …
repeat() 不停的反复一个元素
repeat(20) #--> 20 20 20 20 20 …
repeat(20,3) #--> 20 20 20
有限循环器
accumulate() 将可迭代的容器进行累加,也能够自己指定详细的操作
accumulate(range(10)) #--> 0 1 3 6 10
accumulate(range(1,5), func = operator.mul) #--> 1 2 6 24
chain() 将几个可迭代的容器依次迭代
chain(range(5),'abcd') #--> 0 1 2 3 4 a b c d
chain(['abcd', 'bcde', 'cdef']) #--> abcd bcde cdef
chain.from_iterable() 将可迭代容器里面的元素再次进行迭代一次
chain.from_iterable(['abcd', 'bcde', 'cdef']) #--> a b c d b c d e c d e f
chain.from_iterable([['abcd', 'bcde','cdef'],'abcd']) #--> abcd bcdecdef a b c d
compress() 两个參数分别为data, selectors, 依据selectors中的真假情况返回data中的元素
compress('ABCDEF', [1,0,1,0,1,1]) #--> A C E F
dropwhile() 原型为dropwhile(predicate, iterable)当predicate返回True时,跳过元素。一旦函数返回False,则開始收集剩下的全部元素到循环器
dropwhile(lambda x: x<5, [1,4,6,4,1]) #--> 6 4 1
filterfalse() 原型为filterfalse(predicate, iterable)当predicate返回False时,才将iterable中的元素加入进循环器
filterfalse(lambda x : x%2 , range(10)) #--> 0 2 4 6 8
groupby() 原型为groupby(iterable, key=None)将key的结果作用于iterable中的元素,将拥有同样返回结果的元素增加到循环器中。该函数之前须要确保iterable是经过排序的
演示样例一:
for i,g in itertools.groupby('AAAABBBCCDAABBB'):
print(i,list(g))
结果:
A ['A', 'A', 'A','A']
B ['B', 'B', 'B']
C ['C', 'C']
D ['D']
A ['A', 'A']
B ['B', 'B', 'B']
演示样例二:
def height_classify(h):
if h>180 :
return 'tall'
elif h< 160 :
return 'short'
else :
return 'middle'
friends = [192, 158, 168, 195, 185, 170, 135,174, 182]
friends = sorted(friends, key =height_classify)
for m, n in itertools.groupby(friends, key =height_classify):
print(m)
print(list(n))
结果:
middle
[168, 170, 174]
short
[158, 135]
tall
[192, 195, 185, 182]
islice() 原型为islice(iterable, stop)或islice(iterable, start, stop[, step])
islice('ABCDEFG', 2) #--> A B
islice('ABCDEFG', 2, 4) #--> C D
islice('ABCDEFG', 2, None) #--> C D E F G
islice('ABCDEFG', 0, None, 2) #--> A C E G
starmap() 原型为starmap(function, iterable)
starmap(pow, [(2,5), (3,2), (10,3)]) #--> 32 9 1000
takewhile() 原型为takewhile(predicate,iterable),与dropwhile()功能相反
takewhile(lambda x: x<5, [1,4,6,4,1]) #--> 1 4
tee() 原型为tee(iterable, n=2); 从单个的iterable返回n个独立的循环器
zip_longest() 原型为zip_longest(*iterables,fillvalue=None)
zip_longest('abcd','123',fillvalue='*') #--> ('a', '1')('b','2')('c', '3')('d', '*')
组合生成器
product() 原型为product(*iterables, repeat=1)做笛卡尔乘积
product('abc','xy') #--> ('a', 'x')('a', 'y')('b', 'x')('b', 'y')('c','x')('c', 'y')
permutations() 原型为permutations(iterable,r=None)全排列
permutations('abc') #--> ('a', 'b', 'c')('a', 'c', 'b')('b', 'a', 'c')('b','c', 'a')('c', 'a', 'b')('c', 'b', 'a')
permutations('abcd', r=2) #--> ('a', 'b')('a', 'c')('a', 'd')('b', 'a')('b','c')('b', 'd')('c', 'a')('c', 'b')('c', 'd')('d', 'a')('d', 'b')('d', 'c')
combinations() 原型为combinations(iterable, r)
combinations('abcd',r=3) #--> ('a', 'b', 'c')('a', 'b', 'd')('a', 'c', 'd')('b','c', 'd')
combinations_with_replacement() 原型为combinations_with_replacement(iterable,r)
combinations_with_replacement('abc',r=3) #--> ('a', 'a', 'a')('a', 'a', 'b')('a', 'a', 'c')('a','b', 'b')('a', 'b', 'c')('a', 'c', 'c')('b', 'b', 'b')('b', 'b', 'c')('b', 'c','c')('c', 'c', 'c')