简体   繁体   English

使用具有列表理解功能的嵌套for循环构建列表

[英]Build list using nested for loops with list comprehension

I need to build a list using a list comprehension. 我需要使用列表理解来建立列表。 This is basically what it has to do: 这基本上是它要做的:

pattern = []
for c in range(3):
    for r in range(3):
        if r == c:
            pattern.append(a)
        else:
            pattern.append(b)

But this all somehow needs to be condensed to only one line! 但这一切都只需要压缩为一条线! I have never used a list comprehension before, so please explain your solution. 我以前从未使用过列表理解功能,因此请解释您的解决方案。

Thank you! 谢谢!

Edit: If I wanted the new list to consist of sublists, could that be put into the list comprehension too? 编辑:如果我希望新列表包含子列表,是否也可以将其放入列表理解中? Above I used a range of 3, so in the produced list every sublist would consist of 3 elements, ie 上面我使用的范围是3,因此在产生的列表中,每个子列表将包含3个元素,即

pattern = [['a','b','b'],['b','a','b'],['b','b','a']]

The general form of the list comprehension can be understood like this 列表理解的一般形式可以这样理解

[(the task to be done) (how long it has to be done)]

We normally use for loop in the how long it has to be done part and the task to be done part can have if conditions as well. 我们通常使用for循环来说明必须完成多长时间 ,以及if条件也可以完成的任务 The important thing to be noted is, the task to be done part should return a valid value (even None is a valid value). 要注意的重要一点是, 要完成的任务部分应该返回一个有效值(即使None是有效值)。 So, you cannot use any of the Python statements ( return , in Python 2.x print , etc) in the list comprehension. 因此,您不能在列表推导中使用任何Python语句( return ,在Python 2.x print等中)。

Now to answer your first question, 现在回答您的第一个问题,

['a' if r == c else 'b' for c in range(3) for r in range(3)]
# ['a', 'b', 'b', 'b', 'a', 'b', 'b', 'b', 'a']

This exactly creates a list as you have shown in the for loop version. 正如您在for循环版本中显示的那样,这将精确地创建一个列表。

'a' if r == c else 'b'

This is same as 这和

if r == c:
    'a'
else:
    'b'

First, for c in range(3) will be executed and the list [0, 1, 2] will be generated, and then on every iteration for r in range(3) will be executed and the list [0, 1, 2] will be generated. 首先, for c in range(3)将执行for c in range(3)并生成列表[0, 1, 2] ,然后在每次迭代中for r in range(3)并执行列表[0, 1, 2]将被生成。 On each iteration of r , the if condition we saw above will be executed and the result of that if..else will be used as the element of the new list being generated. r每次迭代中,将执行上面看到的if条件,并且if..else的结果将用作所生成的新列表的元素。

To answer your second question, you can very well use list comprehension. 要回答第二个问题,您可以很好地使用列表理解。

Our basic understanding from the above example, is that list comprehension is to generate a list. 从上面的示例中我们的基本理解是,列表理解是生成列表。 Now, lets try and nest the list comprehensions (we are going to use list comprehension in the the task to be done part), like this 现在,让我们尝试嵌套列表推导(我们将在要完成的任务的一部分中使用列表推导),像这样

[['a' if r == c else 'b' for r in range(3)] for c in range(3)]
# [['a', 'b', 'b'], ['b', 'a', 'b'], ['b', 'b', 'a']]

Now, if you look at our nested list comprehension, first, for c in range(3) will be executed and then ['a' if r == c else 'b' for r in range(3)] part will be executed, which will generate the individual rows of the nested lists. 现在,如果您看一下嵌套列表的理解,将首先for c in range(3)执行,然后['a' if r == c else 'b' for r in range(3)]则执行['a' if r == c else 'b' for r in range(3)]将执行,这将生成嵌套列表的各个行。 The important thing to note here is, c is available within the nested list comprehension. 这里要注意的重要事情是,嵌套列表推导中可以使用c

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

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