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

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基础教程

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。