python有几种循环遍历的方法?
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学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
Python的缩进规则
和其它程序设计语言(如Java、C语言)采用大括号“{}”分隔代码块不同,Python采用代码缩进和冒号(:)来区分代码块之间的层次。在Python中,对于类...详情>>
2023-11-11 23:15:31
python五大异常处理机制
我们明白了什么是异常后,那么发现异常后怎么处理,便是我们接下来要解决的问题。这里将处理异常的方式总结为五种。1、默认异常处理机制“...详情>>
2023-11-11 22:02:33
python中any函数如何使用?
python中any函数如何使用?在python中,如果想要判断一个tuple或者list是否全部为空、0、False,可以使用any函数。使用any函数,当全为空、0、Fa...详情>>
2023-11-11 15:18:19
Python的信号库Blinker有何用法?
python的信号库Blinker有何用法?本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。作为一个信号库,使用时候是支持一对一以及一对多...详情>>
2023-11-11 14:18:20