Till now, we have created a list and accessed individual elements of that list, now its time to learn how to use all of the elements in a list by iterating over them, using loops.
到现在为止,我们已经创建了一个列表并访问了该列表的各个元素 ,现在是时候通过循环使用遍历它们的方式来学习如何使用列表中的所有元素。
When you have a list with hundred or a thousand elements, and you want to access a few of them or all of them to perform some operation or calculation on them, it would be very hectic to use index numbers to access all the elements. In such cases, we use an iterative method, to iterate over all the list items. To understand how to iterate over list elements, we will be using loops. Don't worry loops will be covered in details in Looping tutorial.
当您有一个包含一百或一千个元素的列表,并且想要访问其中的一些或全部元素以对它们执行某些操作或计算时,使用索引号来访问所有元素将非常忙。 在这种情况下,我们使用迭代方法来迭代所有列表项。 为了理解如何遍历列表元素,我们将使用循环。 不用担心, 循环教程将详细介绍循环 。
循环简介 (Introduction to Loops)
Loops are used for doing redundant and repetitive tasks.
循环用于执行冗余和重复的任务。
使用for
循环 (Using for
loop)
Let's start with for
loop. For iterating over a list, a for
loop will need two things - First, a reference variable to store the element being iterated and second, a source (list
or a range()
, as in the previous case). When a for
loop executes, elements of the source are one by one copied to the reference variable for utilizing(performing operations) them inside the loop. For example,
让我们从for
循环开始。 为了遍历一个列表, for
循环将需要两件事-首先,一个用于存储要迭代的元素的引用变量,其次,一个源(如前一种情况那样,为list
(或list
range()
))。 当执行for
循环时,将源元素一一复制到参考变量,以在循环内利用(执行操作)它们。 例如,
for x in range(0,5):
print (x)
0 1 2 3 4
0 1 2 3 4
In the above case, x
is our reference variable in which, each element in the range(0, 5)
is getting stored, iteratively(one by one), simple enough.
在上述情况下, x
是我们的参考变量,其中足够简单地迭代地(一个一个地)存储range(0, 5)
中的每个元素。
Also, you must know that range(0, 5)
returns a list
with elements 0 to 4 in it.
同样,您必须知道range(0, 5)
返回一个包含0到4元素的list
。
使用while
循环 (Using while
loop)
Another way of iterating is by using the while
loop. The while
loop requires one condition to function. The loop keeps on iterating till that condition is true. As soon as it becomes false, the loops get terminated. For example,
迭代的另一种方法是使用while
循环。 while
循环需要一个条件才能起作用。 循环不断迭代直到该条件成立 。 一旦变为false ,循环就会终止。 例如,
i = 0
while i < 5:
print (i);
i = i+1;
0 1 2 3 4
0 1 2 3 4
You can see that both the loops returned the same output, but the while
loop, used the condition i < 5
in order to get the output. In simple english, it implies that "while i
is less than 5
, keep printing i
and keep incrementing the value of i
". Do you know why we increment the value of i
, everytime after printing its value? Well, it's because if we hadn't incremented its value, the value would have remained the same as it was declared initially, which is, i = 0
, and the condition i < 5
, would have always been true and hence the loop would have never ended. Such condition leads to something that we call an infinite loop.
您可以看到两个循环都返回了相同的输出,但是while
循环使用条件i < 5
来获取输出。 用简单的英语来说,它意味着“当i
小于5
,继续打印i
并继续增加i
的值” 。 您知道为什么每次打印i
值后都会增加i
的值吗? 好吧,这是因为如果我们不增加其值,则该值将与最初声明的值保持不变, i = 0
,并且条件i < 5
始终为真 ,因此循环将永无止境。 这种情况导致我们称之为无限循环 。
将循环与列表一起使用 (Using Loops with List)
Enough about loops, now let's see how we can use them to access the List elements. Take a list with any type of element. Now, using the for
loop we can access each element of the list very easily. For accessing the elements, we have to use a reference variable and our list variable as source.
关于循环已经足够了,现在让我们看看如何使用循环来访问List元素。 列出任何类型的元素。 现在,使用for
循环,我们可以非常轻松地访问列表的每个元素。 为了访问元素,我们必须使用参考变量和列表变量作为source 。
myList = ['Last Of Us', 'Doom', 'Dota', 'Halo', ' ']
for x in myList:
print (x)
Last Of Us Doom Dota Halo
最后的我们Doom Dota Halo
同时迭代两个列表zip()
方法 (Iterate two Lists simultaneously - zip()
method)
Suppose there are two lists and you want to ADD each element of the first list serially to each element of the second list and save it to a third (empty) list.
假设有两个列表,并且您想要将第一个列表的每个元素依次添加到第二个列表的每个元素,并将其保存到第三个(空)列表。
We can achieve this by executing a while
loop like shown in below code, but this is not the right way.
我们可以通过执行以下代码所示的while
循环来实现此目的,但这不是正确的方法。
i = 0
A = [1, 2, 3, 4]
B = [5, 6, 7, 8]
C = []
while i < len(A):
C.append(A[i]+B[i]);
i=i+1
In the above code, we will have to add a lot of conditions to make sure that it runs without error all the time. For example, What if the size of both the list is not same?
在上面的代码中,我们将必须添加许多条件以确保其始终无错误运行。 例如,如果两个列表的大小都不相同怎么办?
We will have to do something different in such case because we have to iterate over the elements of two different lists, together. This can be done using the function zip()
.
在这种情况下,我们将不得不做一些不同的事情,因为我们必须一起迭代两个不同列表的元素。 这可以使用zip()
函数来完成。
Suppose the two lists are,
假设这两个列表是
>>> A = [9, 8, 7, 6, 5]
>>> B = [3, 4, 5, 6, 7]
Then, let's create an empty list to save the results.
然后,我们创建一个空列表来保存结果。
>>> C = []
Then the iteration process will require two reference variables, one for each list.
然后,迭代过程将需要两个参考变量,每个列表一个。
for x, y in zip(A, B):
C.append(x+y)
zip()
function can accept any number of list arguments. In this case, we have passed two lists A and B. Since both A and B have 5 elements, hence zip()
will make the loop iterate 5 times. But, what if both the lists have different number of elements? Suppose A have n
elements and B have m
elements, if m < n
then zip()
will make loop m
times, otherwise n
times.
zip()
函数可以接受任意数量的列表参数。 在这种情况下,我们传递了两个列表A和B。 由于A和B都有5个元素,因此zip()
将使循环迭代5次。 但是,如果两个列表都具有不同数量的元素怎么办? 假设A有n
元素,B有m
元素,如果m < n
则zip()
将循环m
次,否则循环n
次。
Another approach to this problem could have been using the index number of the list A and B.
解决此问题的另一种方法可能是使用列表A和B的索引号。
for i in range(0,5):
C.append(A[i]+B[i])
Although while using this technique it is necessary to know the size of both the lists prior to appending, while the previous approach (zip
function) can be used in almost any situation.
尽管在使用此技术时有必要在附加之前知道两个列表的大小,但是在几乎任何情况下都可以使用以前的方法( zip
函数)。
翻译自: https://www.studytonight.com/python/utilising-list-elements