当前位置: 首页 > 图灵资讯 > 行业资讯> python有几种循环遍历的方法?

python有几种循环遍历的方法?

来源:图灵python
时间: 2024-08-14 11:14:14

1、for-In可用于数组和字典的遍历。

words=['cat','window','defenestrate']

forwinwords:
print(w,len(w))

#使用数组访问操作符,数组的副本可以快速生成
forwinwords[:]:
iflen(w)>6:
words.insert(0,w)

#words->['defenestrate','cat','window','defenestrate']

2、要使用数字序列进行遍历,可使用Python内置的range函数。

a=['Mary','had','a','little','lamb']

foriinrange(len(a)):
print(i,a[i])

以上是python2循环遍历的方法,希望对大家有所帮助。更多Python学习指导:基础教程python基础教程

本教程的操作环境:windows7系统,Python 3.9.1,DELL G3电脑。