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电脑。