简体   繁体   English

这个循环如何工作?

[英]How does this for loop work?

I am trying to learn python and I am going through the book programming python. 我正在尝试学习python,我正在阅读编程python。 I know java pretty well so I decided to give python a try as well. 我非常了解java所以我决定尝试python。 I am going through an example using loops and I am confused about what is happening in this code 我将通过一个使用循环的示例,我对此代码中发生的事情感到困惑

for person in people:
  for (name, value) in person:
    if name == 'name': print(value)

I know that there are two loops and person is incremented by 1 each time it loops through, what i dont understand is what is happening to the (name, value) in the second loop. 我知道有两个循环,每次循环时person增加1,我不理解的是第二个循环中(name,value)发生了什么。 Could someone please explain to me what is happening? 有人可以向我解释发生了什么事吗?

Most likely it's supposed to deal with three dimensional list of the following format: 最有可能它应该处理以下格式的三维列表:

people = [
    [['name', 'John'], ['age', 21]],
    [['name', 'Ann'],  ['age', 45]],
    [['name', 'Tom'],  ['age', 32]],
]

for person in people:
  # person is like [['name', 'Ann'],  ['age', 45]] here
  for (name, value) in person:
    # unpack [field_name, field_value] to name = field_name, value = field_value
    # so name is like 'name' (or 'age') and value is like 'Ann' (or 45)
    if name == 'name': print(value)

Information for every person is stored in a list of pairs. 每个人的信息存储在一对列表中。 Iterating over this person list in that manner unpacks every pair into two separate variables: name and value that are later used to check field type and print value if it's name field. 以这种方式迭代此person列表会将每一对解包为两个单独的变量: namevalue ,稍后用于检查字段类型和打印值(如果是名称字段)。

In the result this snippet will print names of all people. 在结果中,此片段将打印所有人的姓名。

No, person is not incremented by 1 each time through the loop. 不,每次循环时person不会增加1。 It's not a number. 这不是一个数字。 person receives the next item from people each time through the loop. person从接收的下一个项目people通过每一次循环中。

The second loop is the same: person is a list (or other sequence) and each iteration through the list retrieves one item from the list. 第二个循环是相同的: person是列表(或其他序列),列表中的每次迭代都从列表中检索一个项目。

Each item in person is a key-value pair, represented as a two-item list (or other sequence; a tuple is likely) with the key first and the value second. person每个项目是键值对,表示为两项列表(或其他序列;可能是元组),其中键首先且值为秒。 Rather than iterating over this list, since we know there are two items, we can unpack the items to the variables name and value (a common Python idiom). 我们不是迭代这个列表,因为我们知道有两个项目,我们可以将项目解压缩到变量namevalue (一个常见的Python习语)。

If we find the key-value pair that begins with "name", then we have found the name of the person, so we print it. 如果我们找到以“name”开头的键值对,那么我们找到了该人的名字,因此我们将其打印出来。

This is a little confusing because the word "name" is used for the key of a key-value pair as well as for the key we're looking for. 这有点令人困惑,因为“name”这个词用于键值对的键以及我们正在寻找的键。 If I were writing this, I would use "key" for the loop variable instead. 如果我写这个,我会使用“key”代替循环变量。

In short, people is a list of lists. 简而言之, people是列表清单。 Each list in people represents a person, and is itself a list of key-value pairs. people每个列表代表一个人,并且本身就是键值对的列表。 Each key-value pair is a list of two items. 每个键值对是两个项目的列表。 The outer loop iterates over people, the inner loop iterates over the key-value pairs in the person. 外循环遍历人,内循环遍历人的键值对。

Assuming person is a list of tuples/lists it is extracting the first and second value. 假设person是元组/列表的列表,它正在提取第一个和第二个值。

It is basically the same as: 它基本上与以下相同:

for x in person:
    name = x[0]
    value = x[1]
    # or
    name, value = x

A for loop has the form: for循环的形式如下:

for vars in iterable:

and vars can be anything that can appear on the left-hand side of an assignment statement. 和vars可以是任何可以出现在赋值语句左侧的东西。 Then each value from the iterable is assigned to that left-hand side. 然后将来自iterable的每个值分配给该左侧。

In Python, you can assign to a number of names at once, in which case the right-hand side must be a list or tuple with that many values: 在Python中,您可以一次分配多个名称,在这种情况下,右侧必须是包含许多值的列表或元组:

a, b, c = 1, 2, 3

So in your for loop, each value from person must be a tuple or list with two elements, a pair. 所以在你的for循环中,person的每个值必须是一个元组或带有两个元素的列表,一对。 Each iteration of the loop, the next pair is unpacked into name and value . 循环的每次迭代,下一对被解压缩为namevalue

In short, "name" and "value" get assigned values from "person" for each element "person" in "people". 简而言之,“名称”和“值”从“人”中为“人”中的每个元素“人”获得指定值。

Let's back up a little bit. 让我们回顾一下。

for person in people:

iterates through the elements of a list named "people". 遍历名为“people”的列表的元素。 Each pass through this loop, the variable named "person" gets assigned the next element from the list named "people". 每次通过此循环时,名为“person”的变量将从名为“people”的列表中分配下一个元素。

for (name, value) in person:

The variable named "person" is a list in which each element is a tuple with two variables named "name" and "value". 名为“person”的变量是一个列表,其中每个元素都是一个元组,其中包含两个名为“name”和“value”的变量。 Each pass through this loop, the current "person" list element gets split up into its constituent parts, "name" and "people". 每次通过这个循环,当前的“人”列表元素被分成其组成部分,“名称”和“人”。

So "people" looks like 所以“人”看起来像

[person1, person2, person3, ...]

and each "person" looks like 每个“人”看起来像

[(name1, value1), (name2, value2), (name3, value3), ...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM