python遍历是什么意思_在Python中遍历列表的方法有哪些
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)
運(yùn)行結(jié)果:['m1', 1900, 'm2', 2000]
二、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)
運(yùn)行結(jié)果:0 m1
1 1900
2 m2
3 2000
當(dāng)從非0下標(biāo)開始遍歷元素的時(shí)候可以用如下方法for i, el in enumerate(lists, 1):
print(i, el)
運(yùn)行結(jié)果:1 m1
2 1900
3 m2
4 2000
更多Python相關(guān)技術(shù)文章,請(qǐng)?jiān)L問Python教程欄目進(jìn)行學(xué)習(xí)!
總結(jié)
以上是生活随笔為你收集整理的python遍历是什么意思_在Python中遍历列表的方法有哪些的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UVA 11572
- 下一篇: websocket python爬虫_p