人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

Python編程itertools模塊處理可迭代集合相關(guān)函數(shù)

發(fā)布日期:2022-01-04 10:32 | 文章來源:站長之家

容器與可迭代對象

在正式開始前先補充一些基本概念在 Python 中存在容器 與 可迭代對象

  • 容器:用來存儲多個元素的數(shù)據(jù)結(jié)構(gòu),例如 列表,元組,字典,集合等內(nèi)容;
  • 可迭代對象:實現(xiàn)了 __iter__ 方法的對象就叫做可迭代對象。

從可迭代對象中還衍生出 迭代器 與 生成器:

  • 迭代器:既實現(xiàn)了 __iter__,也實現(xiàn)了 __next__ 方法的對象叫做迭代器;
  • 生成器:具有 yield 關(guān)鍵字的函數(shù)都是生成器。

這樣就比較清楚了,可迭代對象的范圍要大于容器。而且可迭代對象只能使用一次,使用完畢再獲取值就會提示 StopIteration 異常。

除此之外,可迭代對象還有一些限制:

  • 不能對可迭代對象使用 len 函數(shù);
  • 可以使用 next 方法處理可迭代對象,容器也可以通過 iter 函數(shù)轉(zhuǎn)換成迭代器;
  • for 語句會自動調(diào)用容器的 iter 函數(shù),所以容器也能被循環(huán)迭代。

count() 函數(shù)

count 函數(shù)一般與 range 函數(shù)對比學(xué)習(xí),例如 range 函數(shù)需要定義生成范圍的下限,上限與步長可選,而 count 函數(shù)不同,指定下限與步長,上限值不用聲明。

函數(shù)原型聲明如下

count(start=0, step=1) --> count object

測試代碼如下,其中必須添加跳出循環(huán)的判定條件,否則代碼會一直運行下去。

from itertools import count
a = count(5, 10)
for i in a:
 print(i)
 if i > 100:
  break

除此之外,count 函數(shù)還接收非整數(shù)參數(shù),所以下述代碼中定義的也是正確的。

from itertools import count
a = count(0.5, 0.1)
for i in a:
 print(i)
 if i > 100:
  break

cycle 函數(shù)

cycle 函數(shù)可以循環(huán)一組值,測試代碼如下所示:

from itertools import cycle
x = cycle('夢想橡皮擦abcdf')
for i in range(5):
 print(next(x), end=" ")
print("\n")
print("*" * 100)
for i in range(5):
 print(next(x), end=" ")

代碼輸出如下內(nèi)容:

夢 想 橡 皮 擦
****************************************************************************************************
a b c d f

可以看到 cycle 函數(shù)與 for 循環(huán)非常類似。

repeat 函數(shù)

repeat 函數(shù)用于重復(fù)返回某個值,官方給出的函數(shù)描述如下所示:

class repeat(object):
 """
 repeat(object [,times]) -> create an iterator which returns the object
 for the specified number of times.  If not specified, returns the object
 endlessly.

進行一下簡單的測試,看一下效果:

from itertools import repeat
x = repeat('橡皮擦')
for i in range(5):
 print(next(x), end=" ")
print("\n")
print("*" * 100)
for i in range(5):
 print(next(x), end=" ")

怎么看這個函數(shù),都好像沒有太大用處。

enumerate 函數(shù),添加序號

這個函數(shù)在前面的文章中,已經(jīng)進行過簡單介紹,并且該函數(shù)在 __builtins__ 包中,所以不再過多說明

基本格式如下所示:

enumerate(sequence, [start=0])

其中 start 參數(shù)是下標(biāo)起始位置。

accumulate 函數(shù)

該函數(shù)基于給定的函數(shù)返回一個可迭代對象,默認(rèn)是累加效果,即第二個參數(shù)為 operator.add,測試代碼如下:

from itertools import accumulate
data = [1, 2, 3, 4, 5]
# 計算累積和
print(list(accumulate(data)))  # [1, 3, 6, 10, 15]

針對上述代碼,修改為累積。

from itertools import accumulate
import operator
data = [1, 2, 3, 4, 5]
# 計算累積
print(list(accumulate(data, operator.mul)))

除此之外,第二個參數(shù)還可以為 max,min 等函數(shù),例如下述代碼:

from itertools import accumulate
data = [1, 4, 3, 2, 5]
print(list(accumulate(data, max)))

代碼輸出如下內(nèi)容,其實是將 data 里面的任意兩個值進行了比較,然后留下最大的值。

[1, 4, 4, 4, 5]

chain 與 groupby 函數(shù)

chain 函數(shù)用于將多個迭代器組合為單個迭代器,而 groupby 可以將一個迭代器且分為多個子迭代器。

首先展示一下 groupby 函數(shù)的應(yīng)用:

from itertools import groupby
a = list(groupby('橡橡皮皮擦擦'))
print(a)

輸出內(nèi)容如下所示:

[('橡', <itertools._grouper object at 0x0000000001DD9438>),
('皮', <itertools._grouper object at 0x0000000001DD9278>),
('擦', <itertools._grouper object at 0x00000000021FF710>)]

為了使用 groupby 函數(shù),建議先對原列表進行排序,因為它是有點像切片一樣,發(fā)現(xiàn)不同的就分出一個迭代器。

chain 函數(shù)的用法如下,將多個迭代對象進行拼接:

from itertools import groupby, chain
a = list(chain('ABC', 'AAA', range(1,3)))
print(a)

zip_longest 與 zip

zip 函數(shù)在之前的博客中已經(jīng)進行過說明,zip_longestzip 的區(qū)別就是,zip 返回的結(jié)果以最短的序列為準(zhǔn),而 zip_longest 以最長的為準(zhǔn)。

測試代碼如下,自行比對結(jié)果即可。

from itertools import zip_longest
a = list(zip('ABC', range(5), [10, 20, 30, 40]))
print(a)
a = list(zip_longest('ABC', range(5), [10, 20, 30, 40]))
print(a)

zip_logest 如果碰到長度不一致的序列,缺少部分會填充 None

tee 函數(shù)

tee 函數(shù)可以克隆可迭代對象,產(chǎn)出多個生成器,每個生成器都可以產(chǎn)出輸入的各個元素。

from itertools import tee
a = list(tee('橡皮擦'))
print(a)

compress 函數(shù)

該函數(shù)通過謂詞(是否,True/False)來確定對某個元素的取舍問題,最簡單的代碼如下所示:

from itertools import compress
a = list(compress('橡皮擦', (0, 1, 1)))
print(a)

islice、dropwhile、takewhile、filterfalse、filter

這幾個函數(shù)都是從輸入的可迭代對象中獲取一個子集,而且不修改元素本身。

本部分只羅列各個函數(shù)的原型聲明,具體用法直接參考使用即可。

islice(iterable, stop) --> islice object
islice(iterable, start, stop[, step]) --> islice object
dropwhile(predicate, iterable) --> dropwhile object
takewhile(predicate, iterable) --> takewhile object
filterfalse(function or None, sequence) --> filterfalse object

其中只有 filterfalse 中的參數(shù)是函數(shù)在前,序列在后。

測試代碼如下,尤其注意第一個參數(shù)是 callable 即函數(shù)。

from itertools import islice, dropwhile, takewhile, filterfalse
a = list(filterfalse(lambda x: x in ["皮", "擦"], '橡皮擦'))
print(a)

寫在后面

以上內(nèi)容就是本文的全部內(nèi)容,在使用無限迭代器函數(shù) count,cycle,repeat 的時候,一定要注意即使停止。

以上就是Python編程itertools模塊處理可迭代集合相關(guān)函數(shù)的詳細內(nèi)容,更多關(guān)于Python編程itertools模塊處理可迭代集合函數(shù)的資料請關(guān)注本站其它相關(guān)文章!

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部