成都网站建设设计

将想法与焦点和您一起共享

创新互联Python教程:python有几种循环遍历的方法?

1、for-in可以用来遍历数组与字典。

创新互联于2013年开始,是专业互联网技术服务公司,拥有项目网站设计、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元新建做网站,已为上家服务,为新建各地企业和个人服务,联系电话:18980820575

words = ['cat', 'window', 'defenestrate']
 
for w in words:
    print(w, len(w))
 
# 使用数组访问操作符,能够迅速地生成数组的副本
for w in words[:]:
    if len(w) > 6:
        words.insert(0, w)
 
# words -> ['defenestrate', 'cat', 'window', 'defenestrate']

2、如果希望使用数字序列进行遍历,可以使用python内置的range函数。

a = ['Mary', 'had', 'a', 'little', 'lamb']
 
for i in range(len(a)):
    print(i, a[i])

以上就是python2种循环遍历的方法,希望对大家有所帮助。更多Python学习指路:创新互联python教程

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


网站题目:创新互联Python教程:python有几种循环遍历的方法?
本文地址:https://chengdu.cdxwcx.cn/article/ccoghpo.html