最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

在Python中遍历列表的方法有哪些

来源:动视网 责编:小采 时间:2020-11-27 14:07:30
文档

在Python中遍历列表的方法有哪些

在Python中遍历列表的方法有哪些:Python中遍历列表有以下几种方法: 一、for循环遍历lists = ["m1", 1900, "m2", 2000] for item in lists: print(item)lists = ["m1", 1900, "m2", 2000] for i
推荐度:
导读在Python中遍历列表的方法有哪些:Python中遍历列表有以下几种方法: 一、for循环遍历lists = ["m1", 1900, "m2", 2000] for item in lists: print(item)lists = ["m1", 1900, "m2", 2000] for i


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中遍历列表的方法有哪些:Python中遍历列表有以下几种方法: 一、for循环遍历lists = ["m1", 1900, "m2", 2000] for item in lists: print(item)lists = ["m1", 1900, "m2", 2000] for i
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top