日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

学习笔记——itertools模块

發布時間:2025/3/21 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学习笔记——itertools模块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

官方描述:Functional tools for creating and using iterators.即用于創建高效迭代器的函數。

函數目錄

無限迭代器有限迭代器組合生成器
countchainproduct
cyclecompresspermutations
repeatdropwhile
filterfalsecombinations
groupbycombinations_with_replacement
islice
starmap
takewhile
tee
zip_longest

1、chain

itertools.chain(*iterables) : 將多個序列作為一個單獨的序列返回。

示例:

import itertools for i in itertools.chain('i', 'love', 'python'):print(i,end=' ') ------------------ 結果: i l o v e p y t h o n

2、combinations

itertools.combinations(iterable, r):返回指定長度的”組合”

示例:

import itertools for i in itertools.combinations('abc', 2):print(i,end=' ') --------------------- 結果: ('a', 'b') ('a', 'c') ('b', 'c')

3、combinations_with_replacement

itertools.combinations_with_replacement(iterable, r):返回指定長度的“組合”,組合內元素可重復

示例:

import itertools for i in itertools.combinations_with_replacement('abc', 2):print(i,end=' ') ---------------------- 結果: ('a', 'a') ('a', 'b') ('a', 'c') ('b', 'b') ('b', 'c') ('c', 'c')

4、compress

itertools.compress(data, selectors):返回selector為True的data對應元素

示例:

import itertools for i in itertools.compress('abcd', [1, 0, 1, 0]):print(i,end=' ') ---------------------- 結果: a c

5、count

itertools.count(start=0,step=1):返回以start開始,step遞增的序列,無限遞增

示例:

import itertools for i in itertools.count(start=0, step=2):print(i,end=' ') ---------------------- 結果: a b a b···

6、cycle

itertools.cycle(iterable):將迭代器進行無限迭代

示例:

import itertools for i in itertools.cycle('ab'):print(i,end=' ') ---------------------- 結果: a b a b···

7、dropwhile

itertools.dropwhile(predicate, iterable):直到predicate為真,就返回iterable后續數據, 否則drop掉

示例:

import itertools for i in itertools.dropwhile(lambda x: x<5, [2,1,6,8,2,1]):print(i,end=' ') ---------------------- 結果: 6 8 2 1

8、filterfalse

itertools.ifilter(predicate, iterable):返回predicate結果為True的元素迭代器,如果predicate為None,則返回所有iterable中為True的項

示例:

import itertools for i in itertools.filterfalse(lambda x: x % 2, range(10)):print(i,end=' ') ---------------------- 結果: 0 2 4 6 8

9、groupby

itertools.groupby(iterable[,key]):返回一組(key,itera),key為iterable的值,itera為等于key的所有項

示例:

import itertools for key,value in itertools.groupby('aabbbc'):print(key,':',list(value),end=' ') ---------------------- 結果: a : ['a', 'a'] b : ['b', 'b', 'b'] c : ['c']

10、islice

itertools.islice(iterable, start,stop[,step]):相當于迭代器方式的切片操作

示例:

import itertools for i in itertools.islice('abcdefg', 1, 4, 2):print(i,end=' ') ---------------------- 結果: b d

11、permutations

itertools.premutations(iteravle[,r]):返回長度為r的排列

示例:

import itertools for i in itertools.permutations('abc', 2):print(i,end=' ') ---------------------- 結果: ('a', 'b') ('a', 'c') ('b', 'a') ('b', 'c') ('c', 'a') ('c', 'b')

12、product

itertools.product(*iterable[,repeat]):返回指定長度的所有組合,可理解為笛卡爾乘積

示例:

import itertools for i in itertools.product('abc', repeat=2):print(i,end=' ') ---------------------- 結果: ('a', 'a') ('a', 'b') ('a', 'c') ('b', 'a') ('b', 'b') ('b', 'c') ('c', 'a') ('c', 'b') ('c', 'c')

13、repeat

itertools.repeat(object,[,times]):不停的返回object對象,如果指定了times,則返回times次

示例:

import itertools for i in itertools.repeat('a', 2):print(i,end=' ') ---------------------- 結果: a a

14、starmap

itertools.starmap(function,iterable):返回function(iter)的值,iter為iterable的元素

示例:

import itertools for i in itertools.starmap(lambda x,y: x*y,[(1, 2),(3, 4)]):print(i,end=' ') ---------------------- 結果: 2 12

15、takewhile

itertools.takewhile(predicate,iterable):如果predicate為真,則返回iterable元素,如果為假則不再返回,break

示例:

import itertools for i in itertools.takewhile(lambda x: x<5,[1,3,5,6]):print(i,end=' ') ---------------------- 結果: 1 3

16、tee

itertools.tee(iterable, n=2): 用于從 iterable 創建 n 個獨立的迭代器,以元組的形式返回, n 的默認值是 2

示例:

import itertools for i in itertools.tee('abcd'):print(list(i),end=' ') ---------------------- 結果: ['a', 'b', 'c', 'd'] ['a', 'b', 'c', 'd']

17、zip_longest

itertools.zip_longest(*iterables, fillvalue=None):若可迭代對象的長度未對齊,如果有指定 fillvalue ,則會用其填充缺失的值,否則為 None 。

示例:

import itertools for i in itertools.zip_longest('ABCD', 'xy'):print(list(i),end=' ') ---------------------- 結果: ['A', 'x'] ['B', 'y'] ['C', None] ['D', None]

總結

以上是生活随笔為你收集整理的学习笔记——itertools模块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。