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')
?
總結
以上是生活随笔為你收集整理的Python itertools模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 跟着实例学习设计模式(7)-原型模式pr
- 下一篇: 【源资讯 第36期】赶超 Java 和