Python如何有效地使用迭代
1. C型方法
C型方法:這種方法需要迭代總數(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
注意事項:
Python程序員很少使用這種類型的循環(huán)。
這個4步方法沒有創(chuàng)建單一視圖循環(huán)結(jié)構(gòu)的緊湊性。
這在大規(guī)模的程序或設(shè)計中也容易出錯。
Python中沒有C風(fēng)格的for循環(huán),即(int i=0; i<n; i++)
2. for … in 方法
for-in(或for each)樣式的方法:這種風(fēng)格在Python中使用,包含列表迭代器,字典,n維數(shù)組等。迭代器獲取每個組件并在循環(huán)時打印數(shù)據(jù)。迭代器在這個構(gòu)造中自動遞增/遞減。
# 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()來進(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是一個內(nèi)置的Python函數(shù),它將輸入作為迭代器、列表等,并返回一個包含索引和迭代器序列中該索引處的數(shù)據(jù)的元組。例如,enumerate(cars)返回一個迭代器,該迭代器將返回(0,cars[0])、(1,cars[1])、(2,cars[2]),依此類推。
# 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()有助于嵌入用于訪問迭代器中的每個數(shù)據(jù)項的解決方案,并獲取每個數(shù)據(jù)項的索引。
循環(huán)擴(kuò)展:
i)單個循環(huán)構(gòu)造的兩個迭代器:在這種情況下,列表和字典將用于使用enumerate函數(shù)的單個循環(huán)塊中的每次迭代。讓我們看一個例子。
# 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ù)(兩個迭代器都用于單個循環(huán)構(gòu)造):此函數(shù)有助于在第i個位置類似類型的迭代器(列表-列表或dict- dict等)數(shù)據(jù)項。它使用這些輸入迭代器的最短長度。其他更長的迭代器項將被跳過。如果迭代器為空,則返回No output。
例如,對兩個列表(迭代器)使用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ù)中的這些迭代器的反向操作稱為使用“*”運算符解包。enumerate函數(shù)和zip函數(shù)的使用有助于實現(xiàn)python中迭代邏輯的有效擴(kuò)展,解決了一個巨大任務(wù)或問題的更多子問題。
# 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用OpenCV對車道進(jìn)行實時檢測的實現(xiàn)示例代碼
這篇文章主要介紹了使用OpenCV對車道進(jìn)行實時檢測的實現(xiàn)示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06pyCaret效率倍增開源低代碼的python機(jī)器學(xué)習(xí)工具
這篇文章主要介紹了pyCaret一款可以使效率倍增的開源低代碼的python機(jī)器學(xué)習(xí)工具,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11python目標(biāo)檢測實現(xiàn)黑花屏分類任務(wù)示例
這篇文章主要為大家介紹了python目標(biāo)檢測實現(xiàn)黑花屏分類任務(wù)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Python Django框架介紹之模板標(biāo)簽及模板的繼承
今天給大家?guī)鞵ython Django框架的相關(guān)知識,文中對模板標(biāo)簽及模板的繼承介紹的非常詳細(xì),對正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05python實現(xiàn)批量轉(zhuǎn)換圖片為黑白
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)批量轉(zhuǎn)換圖片為黑白,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06