Python Lists basically serve the purpose of arrays and store data/elements in it.

Python列表基本上是用于数组的目的,并在其中存储数据/元素。

Lists can be traversed through a number of ways.

可以通过多种方式遍历列表。



在Python中迭代列表的方法 (Ways to iterate List in Python)

  • By using for Loop通过使用for循环
  • By using while Loop通过使用while循环
  • By using List Comprehension通过使用列表理解
  • By using for loop and range() function通过使用for循环和range()函数
  • By using NumPy通过使用NumPy
  • By using enumerate() function通过使用enumerate()函数


1.使用for循环迭代列表 (1. Iterating List using for Loop)

Python for loop can be used to iterate through the List.

Python for loop可用于遍历List。

Example:

例:


input_list = [10, "Safa", 15, "Aman", 1] for x in input_list: print(x) 

Output:

输出:

10
Safa
15
Aman
1


2.通过while循环迭代列表 (2. Iteration of list through while Loop)

Python while loop can be used to iterate through the list.

Python的while循环可用于遍历列表。

Example:

例:


input_list = [10, "Safa", 15, "Aman", 1] length_list = len(input_list)
x = 0while x < length_list: print(input_list[x]) x += 1

Output:

输出:

10
Safa
15
Aman
1


3.列表理解以迭代Python列表 (3. List Comprehension to iterate Python List)

Python List Comprehension can also be used to traverse through a list efficiently.

Python List Comprehension也可以用于有效地遍历列表。

List Comprehension is an easier way to create and traverse a list.

列表理解是创建和遍历列表的简便方法。

Example:

例:


input_list = [10, "Safa", 15, "Aman", 1]
[print(x) for x in input_list]

Output:

输出:

10
Safa
15
Aman
1


4.使用for Loop和range()函数的Python迭代列表 (4. Python iterate List using for Loop and range() function)

The range() method enables the user to create series of elements within a specified range.

使用range()方法 ,用户可以创建指定范围内的一系列元素。

Python for Loop along with range() function can be used to iterate through the list.

Python for Loop和range()函数可用于遍历列表。

Example:

例:


input_list = [10, "Safa", 15, "Aman", 1]
length_list = len(input_list) for x in range(length_list): print(input_list[x])

Output:

输出:

10
Safa
15
Aman
1


5.使用NumPy的Python迭代列表 (5. Python Iterate List using NumPy)

Python NumPy is basically a library that can be used to perform manipulations and operations on huge amount of data, serving the functionality of arrays.

Python NumPy基本上是一个库,可用于对大量数据执行操作和操作,并提供数组功能。

NumPy can be used to traverse list having huge amount of data.

NumPy可用于遍历具有大量数据的列表。

Example:

例:


import numpy as nx = n.arange(12) x = x.reshape(3, 4) for i in n.nditer(x): print(i) 

In the above example, numpy.arange(value) function helps return evenly spaced items in an array according to the argument value provided.

在上面的示例中, numpy.arange(value) 函数根据提供的参数值帮助返回数组中均匀间隔的项目。

The reshape() function enables the user to provide a new shape to the existing array without changing the data inserted into it by providing the argument values to it.

通过reshape()函数 ,用户可以通过向其提供参数值来为现有数组提供新形状,而无需更改插入其中的数据。

The numpy.nditer is basically an iterator object that is used to traverse a list/array.

numpy.nditer基本上是一个迭代器对象,用于遍历列表/数组。

Output:

输出:

0
1
2
3
4
5
6
7
8
9
10
11


6. Python enumerate()函数迭代列表 (6. Python enumerate() function to iterate through a List)

Python enumerate() function basically serves as an easy technique to traverse/iterate through a list.

Python enumerate()函数基本上是一种遍历/迭代列表的简单技术。

Example:

例:


input_list = [10, "Safa", 15, "Aman", 1]
for x, result in enumerate(input_list): print (x, ":",result)

Output:

输出:

0 : 10
1 : Safa
2 : 15
3 : Aman
4 : 1


同时遍历多个列表 (Iterating through multiple lists in a simultaneous manner)

Python zip() function is used to traverse multiple lists simultaneously.

Python zip()函数用于同时遍历多个列表。

It basically takes the smaller of all the lists into consideration and gives the output accordingly.

它基本上会考虑所有列表中较小的一个,并相应地提供输出。

The zip() function halts if anyone of the lists get exhausted or traversed.

如果任何一个列表用尽或遍历,zip()函数将停止。

Example:

例:


import itertools  age = [21, 28, 31]
gender = ['Male', 'Female', 'Others']
city = ['Pune', 'Mumbai'] for (x, y, z) in zip(age, gender, city): print (x, y, z) 

Output:

输出:

21 Male Pune
28 Female Mumbai


结论 (Conclusion)

Thus, in this article, we have understood and implemented different ways to iterate through a Python List.

因此,在本文中,我们已经理解并实现了通过Python列表进行迭代的不同方法。



参考资料 (References)

  • Python List DocumentationPython列表文档

翻译自: https://www.journaldev.com/34197/python-iterate-list

Python迭代列表相关推荐

  1. python迭代列表_Python迭代列表中列的元素

    嗨,所有python新手更习惯于矩阵式软件 我在遍历列表列表时遇到问题.在 我导入了一个csv文件作为列表列表['2004', '1', '7', '20', '50', '0', '8.92', ' ...

  2. python迭代列表_迭代建立列表的最python方法?

    我试图用Python做一些事情,使用下面的一般过程,我想知道实现这一点的最佳方法是什么.在 首先,初始化步骤:创建项目M 创建一个列表L并将M添加到L中 其次,循环以下内容:通过修改添加到L的最后一个 ...

  3. python最新版本 效率_Python:迭代列表与dict项目效率

    这取决于您使用的是哪个版本的Python.在Python 2中,some_dict.items()创建一个新列表,这会占用一些额外的时间并占用额外的内存.另一方面,一旦创建了列表,它就是一个列表,因此 ...

  4. 迭代列表不要For循环,这是Python列表推导式最基本的概念

    如果你还在使用 For 循环迭代列表,那么你需要了解了解列表推导式,看看它的基本概念都是什么. 列表解析式(List comprehension)或者称为列表推导式,是 Python 中非常强大和优雅 ...

  5. python用函数对列表删除_关于python:如何使用range()函数从列表中删除项目时迭代列表?...

    本问题已经有最佳答案,请猛点这里访问. 这是我在学习Python编程时遇到的最常见的问题.问题是,当我尝试使用"range()"函数迭代列表时,检查列表中的给定项是否满足给定条件, ...

  6. python里面列表可以同时删除吗_在python中从列表中删除项,同时对其进行迭代

    本问题已经有最佳答案,请猛点这里访问. 我正在为锦标赛应用程序编写循环算法. 当玩家数量为奇数时,我将'DELETE'添加到玩家列表中,但稍后,当我想从包含'DELETE'的日程表列表中删除所有项目时 ...

  7. python 列表迭代_Python | 以相反的顺序迭代列表

    python 列表迭代 Given a list and we have to iterate it in reverse order in python. 给定一个列表,我们必须在python中以相 ...

  8. 完全理解 Python 迭代对象、迭代器、生成器(转)

    完全理解 Python 迭代对象.迭代器.生成器 本文源自RQ作者的一篇博文,原文是Iterables vs. Iterators vs. Generators » nvie.com,俺写的这篇文章是 ...

  9. python代码大全表解释-python操作列表的函数使用代码详解

    python的列表很重要,学习到后面你会发现使用的地方真的太多了.最近在写一些小项目时经常用到列表,有时其中的方法还会忘哎! 所以为了复习写下了这篇博客,大家也可以来学习一下,应该比较全面和详细了 列 ...

最新文章

  1. 程序员如何解决工作中的技术问题?
  2. GDCM:gdcm::Defs的测试程序
  3. HDFS的API操作-获取文件列表信息
  4. c 指针打印变量_C程序打印不同类型的指针变量的大小。
  5. OpenShift 4 之AMQ Streams(4) - 用Prometheus监控Kafka
  6. python创建应用程序窗口_如何设置pythonwxwidgets应用程序窗口的类名?
  7. 一般信道容量迭代算法c语言,离散信道容量的迭代算法
  8. 极客学院 HTML5
  9. codeblocks设置代码黑色主题
  10. SQLServer 删除表中重复数据(除ID不同的)
  11. 马云说的新零售是错的【完善版】
  12. uniapp微信小程序使用canvas自定义分享名片
  13. 问题 M: 【枚举】桐桐数
  14. matlab包络分析函数,数据包络分析 (MATLAB代码).doc
  15. tp3.2.3 图片打水印
  16. 【xss-labs】闯关记录16~18
  17. 有趣的“灵魂”库集合
  18. 全球与中国高可见度(Hi-Vis)服装市场深度研究分析报告
  19. 数据仓库、OLAP和数据立方体
  20. 手机相机好坏测试软件,教你一分钟教你看懂手机拍照好坏!

热门文章

  1. zookeeper3.4.6完全分布式安装
  2. MyEclipse Web 项目导入 Eclipse 中需要改的文件
  3. C++ Pitfalls 之 reference to an object in a dynamically allocated containter
  4. 密码输入页面的实现-模仿支付宝
  5. [转载] python 需求清单_Python清单操作摘要
  6. [转载] pandas入门:Series、DataFrame、Index基本操作都有了!
  7. [转载] strtol() -- 将字符串转换成长整型数(转载)
  8. [转载] Python3.X之——卷积计算
  9. MATLAB中zero和ones函数(转载)
  10. 内存cgroup---CGroup中参数由来篇