Python教程

【划重点】Python遍历列表的四种方法

本文主要是介绍【划重点】Python遍历列表的四种方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、通过for循环直接遍历

user1 = ["宋江","林冲","卢俊义","吴用"]
for user in user1:
    print(user)

 二、通过range函数

user1 = ["宋江","林冲","卢俊义","吴用"]
for i in range(len(user1)):
    print(i, user1[i])

 三、通过iter迭代器

user1 = ["宋江","林冲","卢俊义","吴用"]
for x in iter(user1):
 print(x)

 四、通过enumerate函数

user1 = ["宋江","林冲","卢俊义","吴用"]
for col,field in enumerate(user1):
    print(col,field)

五、准备睡觉了

 

 

这篇关于【划重点】Python遍历列表的四种方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!