Python如何有效地使用迭代
1. C型方法
C型方法:這種方法需要迭代總數(shù)的先驗(yàn)知識(shí)。
# A C-style way of accessing list elements cars = ["Aston", "Audi", "McLaren"] i = 0 while (i < len(cars)): print(cars[i]) i += 1
輸出
Aston
Audi
McLaren
注意事項(xiàng):
Python程序員很少使用這種類(lèi)型的循環(huán)。
這個(gè)4步方法沒(méi)有創(chuàng)建單一視圖循環(huán)結(jié)構(gòu)的緊湊性。
這在大規(guī)模的程序或設(shè)計(jì)中也容易出錯(cuò)。
Python中沒(méi)有C風(fēng)格的for循環(huán),即(int i=0; i<n; i++)
2. for … in 方法
for-in(或for each)樣式的方法:這種風(fēng)格在Python中使用,包含列表迭代器,字典,n維數(shù)組等。迭代器獲取每個(gè)組件并在循環(huán)時(shí)打印數(shù)據(jù)。迭代器在這個(gè)構(gòu)造中自動(dòng)遞增/遞減。
# Accessing items using for-in loop cars = ["Aston", "Audi", "McLaren"] for x in cars: print(x)
輸出
Aston
Audi
McLaren
3. range函數(shù)
使用range函數(shù)進(jìn)行索引:我們也可以在Python中使用range()來(lái)進(jìn)行索引。
# Accessing items using indexes and for-in cars = ["Aston", "Audi", "McLaren"] for i in range(len(cars)): print(cars[i])
輸出
Aston
Audi
McLaren
4. enumerate
enumerate是一個(gè)內(nèi)置的Python函數(shù),它將輸入作為迭代器、列表等,并返回一個(gè)包含索引和迭代器序列中該索引處的數(shù)據(jù)的元組。例如,enumerate(cars)返回一個(gè)迭代器,該迭代器將返回(0,cars[0])、(1,cars[1])、(2,cars[2]),依此類(lèi)推。
# Accessing items using enumerate() cars = ["Aston", "Audi", "McLaren "] for i, x in enumerate(cars): print(x)
輸出
Aston
Audi
McLaren
下面的解決方案也可以。
# Accessing items and indexes enumerate() cars = ["Aston" , "Audi", "McLaren "] for x in enumerate(cars): print (x[0], x[1])
輸出
(0, 'Aston')
(1, 'Audi')
(2, 'McLaren ')
我們也可以直接打印enumerate()的返回值,看看它返回了什么。
# Printing return value of enumerate() cars = ["Aston" , "Audi", "McLaren "] print (enumerate(cars))
輸出
<enumerate object at 0x7fe4f914d3c0>
enumerate采用參數(shù)start,默認(rèn)設(shè)置為零。我們可以將此參數(shù)更改為任何我們喜歡的值。在下面的代碼中,我們使用start 為1。
# demonstrating the use of start in enumerate cars = ["Aston" , "Audi", "McLaren "] for x in enumerate(cars, start=1): print (x[0], x[1])
輸出
(1, 'Aston')
(2, 'Audi')
(3, 'McLaren ')
enumerate()有助于嵌入用于訪(fǎng)問(wèn)迭代器中的每個(gè)數(shù)據(jù)項(xiàng)的解決方案,并獲取每個(gè)數(shù)據(jù)項(xiàng)的索引。
循環(huán)擴(kuò)展:
i)單個(gè)循環(huán)構(gòu)造的兩個(gè)迭代器:在這種情況下,列表和字典將用于使用enumerate函數(shù)的單個(gè)循環(huán)塊中的每次迭代。讓我們看一個(gè)例子。
# Two separate lists cars = ["Aston", "Audi", "McLaren"] accessories = ["GPS kit", "Car repair-tool kit"] # Single dictionary holds prices of cars and # its accessories. # First three items store prices of cars and # next two items store prices of accessories. prices = {1: "570000$", 2: "68000$", 3: "450000$", 4: "8900$", 5: "4500$"} # Printing prices of cars for index, c in enumerate(cars, start=1): print("Car: %s Price: %s" % (c, prices[index])) # Printing prices of accessories for index, a in enumerate(accessories, start=1): print("Accessory: %s Price: %s" % (a, prices[index+len(cars)]))
輸出
Car: Aston Price: 570000$
Car: Audi Price: 68000$
Car: McLaren Price: 450000$
Accessory: GPS kit Price: 8900$
Accessory: Car repair-tool kit Price: 4500$
ii)zip函數(shù)(兩個(gè)迭代器都用于單個(gè)循環(huán)構(gòu)造):此函數(shù)有助于在第i個(gè)位置類(lèi)似類(lèi)型的迭代器(列表-列表或dict- dict等)數(shù)據(jù)項(xiàng)。它使用這些輸入迭代器的最短長(zhǎng)度。其他更長(zhǎng)的迭代器項(xiàng)將被跳過(guò)。如果迭代器為空,則返回No output。
例如,對(duì)兩個(gè)列表(迭代器)使用zip。
# Python program to demonstrate the working of zip # Two separate lists cars = ["Aston", "Audi", "McLaren"] accessories = ["GPS", "Car Repair Kit", "Dolby sound kit"] # Combining lists and printing for c, a in zip(cars, accessories): print("Car: %s, Accessory required: %s" % (c, a))
輸出
Car: Aston, Accessory required: GPS
Car: Audi, Accessory required: Car Repair Kit
Car: McLaren, Accessory required: Dolby sound kit
zip函數(shù)中的這些迭代器的反向操作稱(chēng)為使用“*”運(yùn)算符解包。enumerate函數(shù)和zip函數(shù)的使用有助于實(shí)現(xiàn)python中迭代邏輯的有效擴(kuò)展,解決了一個(gè)巨大任務(wù)或問(wèn)題的更多子問(wèn)題。
# Python program to demonstrate unzip (reverse # of zip)using * with zip function # Unzip lists l1,l2 = zip(*[('Aston', 'GPS'), ('Audi', 'Car Repair'), ('McLaren', 'Dolby sound kit') ]) # Printing unzipped lists print(l1) print(l2)
輸出
('Aston', 'Audi', 'McLaren')
('GPS', 'Car Repair', 'Dolby sound kit')
到此這篇關(guān)于Python如何有效地使用迭代的文章就介紹到這了,更多相關(guān)Python迭代內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用OpenCV對(duì)車(chē)道進(jìn)行實(shí)時(shí)檢測(cè)的實(shí)現(xiàn)示例代碼
這篇文章主要介紹了使用OpenCV對(duì)車(chē)道進(jìn)行實(shí)時(shí)檢測(cè)的實(shí)現(xiàn)示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06pyCaret效率倍增開(kāi)源低代碼的python機(jī)器學(xué)習(xí)工具
這篇文章主要介紹了pyCaret一款可以使效率倍增的開(kāi)源低代碼的python機(jī)器學(xué)習(xí)工具,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11python目標(biāo)檢測(cè)實(shí)現(xiàn)黑花屏分類(lèi)任務(wù)示例
這篇文章主要為大家介紹了python目標(biāo)檢測(cè)實(shí)現(xiàn)黑花屏分類(lèi)任務(wù)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Python Django框架介紹之模板標(biāo)簽及模板的繼承
今天給大家?guī)?lái)Python Django框架的相關(guān)知識(shí),文中對(duì)模板標(biāo)簽及模板的繼承介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-0510款最好的Web開(kāi)發(fā)的 Python 框架
這篇文章主要介紹了10款最好的Web開(kāi)發(fā)的 Python 框架,總結(jié)的都是非常常用的而且評(píng)價(jià)都非常不錯(cuò)的框架,需要的朋友可以參考下2015-03-03python實(shí)現(xiàn)批量轉(zhuǎn)換圖片為黑白
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)批量轉(zhuǎn)換圖片為黑白,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06