在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)
二、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下标开始遍历元素的时候可以用如下方法
for i, el in enumerate(lists, 1):
print(i, el)
相关推荐
xiesheng 2020-08-06
Tristahong 2020-08-05
meltsnow 2020-06-08
cas的无名 2020-05-30
pythonxuexi 2020-05-21
czsay 2020-05-20
elizabethxxy 2020-11-06
pythonxuexi 2020-10-30
retacnyue 2020-09-28
pythonxuexi 2020-09-06
Morelia 2020-09-04
zhaobig 2020-08-17
linkequa 2020-08-16
CloudXli 2020-08-14
kikaylee 2020-08-12
LowisLucifer 2020-08-09
CatherineC00 2020-08-01