新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:基礎(chǔ)學(xué)習(xí):python遍歷循環(huán)方法有哪些?怎么用?
代碼是一個(gè)非常靈活的的東西,很多代碼樣子不一致,但是表達(dá)的意義確是基本一致的,這就跟我們接下來(lái)要了解的多種循環(huán)遍歷方法一樣,一起來(lái)看下吧~

python中遍歷列表有以下幾種方法:
一、for循環(huán)遍歷
lists = ["m1", 1900, "m2", 2000] for item in lists: print(item) lists = ["m1", 1900, "m2", 2000] for item in lists: item = 0; print(lists)
二、while循環(huán)遍歷:
lists = ["m1", 1900, "m2", 2000] count = 0 while count < len(lists): print(lists[count]) count = count + 1
三、索引遍歷:
for index in range(len(lists)): print(lists[index])
四、使用iter()
for val in iter(lists): print(val)
五、enumerate遍歷方法
for i, val in enumerate(lists): print(i, val)
當(dāng)從非0下標(biāo)開始遍歷元素的時(shí)候可以用如下方法
for i, el in enumerate(lists, 1): print(i, el)
以上就是關(guān)于python循環(huán)遍歷的全部?jī)?nèi)容了,保存下來(lái)試試吧~如需了解更多python實(shí)用知識(shí),點(diǎn)擊進(jìn)入Python學(xué)習(xí)網(wǎng)教學(xué)中心。
(推薦操作系統(tǒng):windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。)
分享標(biāo)題:創(chuàng)新互聯(lián)Python教程:基礎(chǔ)學(xué)習(xí):python遍歷循環(huán)方法有哪些?怎么用?
當(dāng)前鏈接:http://www.fisionsoft.com.cn/article/dhocpdp.html


咨詢
建站咨詢
