Python迭代列表

本文介绍了在Python中迭代列表的六种方法,包括使用for循环、while循环、列表推导式、for循环与range()函数结合、NumPy库以及enumerate()函数。还讲述了如何同时遍历多个列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 = 0
  
while 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 n

x = 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)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值