您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页python有几种循环遍历的方法?

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

来源:二三四教育网

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

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学习指路:

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

本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。

热门图文

Copyright © 2019-2025 how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务