python遍历是什么意思_在Python中遍历列表的方法有哪些
Python中遍歷列表有以下幾種方法:
一、for循環遍歷lists = ["m1", 1900, "m2", 2000]
for item in lists:
print(item)lists = ["m1", 1900, "m2", 2000]
for item in lists:
item = 0;
print(lists)
運行結果:['m1', 1900, 'm2', 2000]
二、while循環遍歷: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)
運行結果:0 m1
1 1900
2 m2
3 2000
當從非0下標開始遍歷元素的時候可以用如下方法for i, el in enumerate(lists, 1):
print(i, el)
運行結果:1 m1
2 1900
3 m2
4 2000
更多Python相關技術文章,請訪問Python教程欄目進行學習!
總結
以上是生活随笔為你收集整理的python遍历是什么意思_在Python中遍历列表的方法有哪些的全部內容,希望文章能夠幫你解決所遇到的問題。