当前位置: 首页 > 图灵资讯 > 行业资讯> 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电脑。

相关文章

如何让vim支持python3

如何让vim支持python3

2025-09-12
python2.7和3.6区别有哪些

python2.7和3.6区别有哪些

2025-09-12
python3有serial库吗

python3有serial库吗

2025-09-12
python中w、r表示什么意思

python中w、r表示什么意思

2025-09-12
python中如何把list变成字符串

python中如何把list变成字符串

2025-09-12
python命名空间是什么

python命名空间是什么

2025-09-12