• 技术文章 >Python技术 >Python基础教程

    基础学习:python遍历循环方法有哪些?怎么用?

    2021-04-08 17:36:21原创3134
    代码是一个非常灵活的的东西,很多代码样子不一致,但是表达的意义确是基本一致的,这就跟我们接下来要了解的多种循环遍历方法一样,一起来看下吧~

    Python中遍历列表有以下几种方法:

    一、for循环遍历

    lists = ["m1", 1900, "m2", 2000]
    for item in lists:
    print(item)
    lists = ["m1", 1900, "m2", 2000]
    for item in lists:
    item = 0;
    print(lists)

    二、while循环遍历:

    lists = ["m1", 1900, "m2", 2000]
    count = 0
    while count < len(lists):
    print(lists[count])
    count = count + 1

    三、索引遍历:

    for index in range(len(lists)):
    print(lists[index])

    四、使用iter()

    for val in iter(lists):
    print(val)

    五、enumerate遍历方法

    for i, val in enumerate(lists):
    print(i, val)

    当从非0下标开始遍历元素的时候可以用如下方法

    for i, el in enumerate(lists, 1):
    print(i, el)

    以上就是关于python循环遍历的全部内容了,保存下来试试吧~如需了解更多python实用知识,点击进入Python学习网教学中心

    (推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

    专题推荐:python 遍历循环方法
    品易云
    上一篇:Python replace()函数:替换字符串中的某个字符 下一篇:基础学习:python中map函数是什么?怎么用?

    相关文章推荐

    • Python subprocess模块怎么用?• 基础教程:python isinstance与type函数用法• Python集成ActiveMQ怎么用?如何连接?• Python replace()函数:替换字符串中的某个字符

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网