python list comprehension double for [英] python list comprehension double for

查看:99
本文介绍了python list comprehension double for的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vec = [[1,2,3], [4,5,6], [7,8,9]]
print [num for elem in vec for num in elem]      <----- this

>>> [1, 2, 3, 4, 5, 6, 7, 8, 9]

这欺骗了我.
我了解elem是来自for elem in vic
的列表中的列表 我不太了解numfor num in elem在开头和结尾的用法.

This is tricking me out.
I understand elem is the lists inside of the list from for elem in vic
I don't quite understand the usage of num and for num in elem in the beginning and the end.

python如何解释呢?
它的顺序是什么?

How does python interpret this?
What's the order it looks at?

推荐答案

让我们对其进行分解.

简单的列表理解:

[x for x in collection]

如果我们将它分成几部分,这很容易理解:[A for B in C]

This is easy to understand if we break it into parts: [A for B in C]

  • A是将出现在结果列表中的项目
  • B是集合C
  • 中的每个项目
  • C是集合本身.
  • A is the item that will be in the resulting list
  • B is each item in the collection C
  • C is the collection itself.

这样,人们可以这样写:

In this way, one could write:

[x.lower() for x in words]

为了将列表中的所有单词都转换为小写.

In order to convert all words in a list to lowercase.

这是我们将其与另一个列表类似的情况:

It is when we complicate this with another list like so:

[x for y in collection for x in y] # [A for B in C for D in E]

在这里,发生了一些特别的事情.我们希望最终列表包括A个项目,并且A个项目位于B个项目内,因此我们必须告诉列表理解这一点.

Here, something special happens. We want our final list to include A items, and A items are found inside B items, so we have to tell the list-comprehension that.

  • A是将出现在结果列表中的项目
  • B是集合C
  • 中的每个项目
  • C是集合本身
  • D是集合E中的每个项目(在本例中也是A)
  • E是另一个集合(在本例中为B)
  • A is the item that will be in the resulting list
  • B is each item in the collection C
  • C is the collection itself
  • D is each item in the collection E (in this case, also A)
  • E is another collection (in this case, B)

此逻辑类似于普通的for循环:

This logic is similar to the normal for loop:

for y in collection:     #      for B in C:
    for x in y:          #          for D in E: (in this case: for A in B)
        # receive x      #              # receive A


要对此进行扩展,并给出一个很好的示例+解释,假设有火车.


To expand on this, and give a great example + explanation, imagine that there is a train.

火车引擎(最前面)总是在那儿(列表理解的结果)

The train engine (the front) is always going to be there (the result of the list-comprehension)

然后,有任意数量的火车车厢,每辆火车车厢的形式为:for x in y

Then, there are any number of train cars, each train car is in the form: for x in y

列表理解可能是这样的:

A list comprehension could look like this:

[z for b in a for c in b for d in c ... for z in y]

这就像有一个常规的for循环:

Which would be like having this regular for-loop:

for b in a:
    for c in b:
        for d in c:
            ...
                for z in y:
                    # have z

换句话说,在列表理解中,您无需添加一行并缩进,而只需将下一个循环添加到末尾即可.

In other words, instead of going down a line and indenting, in a list-comprehension you just add the next loop on to the end.

回到火车的比喻:

Engine-Car-Car-Car ... Tail

尾巴是什么?在列表理解中,尾巴是很特别的事情.您不需要 一个,但是如果您有一条尾巴,那么该尾巴就是一个条件,请看以下示例:

What is the tail? The tail is a special thing in list-comprehensions. You don't need one, but if you have a tail, the tail is a condition, look at this example:

[line for line in file if not line.startswith('#')] 

这将为您提供文件中的每一行,只要该行不是以井号(#)开头,其他行就会被跳过.

This would give you every line in a file as long as the line didn't start with a hashtag (#), others are just skipped.

使用火车尾巴"的技巧是,在所有循环中都有最终的引擎"或结果"时,同时检查它是否为真"/假",上面的示例定期进行for循环如下所示:

The trick to using the "tail" of the train is that it is checked for True/False at the same time as you have your final 'Engine' or 'result' from all the loops, the above example in a regular for-loop would look like this:

for line in file:
    if not line.startswith('#'):
        # have line

请注意:尽管按照我的说法,火车的末尾只有一个尾巴",但条件或尾巴"可以出现在每个 之后>汽车"或循环车...

please note: Though in my analogy of a train there is only a 'tail' at the end of the train, the condition or 'tail' can be after every 'car' or loop...

例如:

>>> z = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
>>> [x for y in z if sum(y)>10 for x in y if x < 10]
[5, 6, 7, 8, 9]

在常规循环中:

>>> for y in z:
    if sum(y)>10:
        for x in y:
            if x < 10:
                print x

5
6
7
8
9

这篇关于python list comprehension double for的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆