简体   繁体   English

以下列表理解的解释是什么?

[英]What is the explanation of the following list comprehension?

input_shape = tuple([i for i in input_shape if i is not None])

I am new to python and don't understand what's happening there. 我是python新手,不了解那里发生了什么。 As I see it is possible to be some analog of C 's int a = 5 > 2 ? val1 : val2; 如我所见,可能是Cint a = 5 > 2 ? val1 : val2;类似物int a = 5 > 2 ? val1 : val2; int a = 5 > 2 ? val1 : val2; , but I can't figure it out. ,但我不知道。

I tried to separate it on small parts to understand, but it still makes no sense for me. 我试图将其分成很小的部分以进行理解,但对我而言仍然没有意义。 Like that: 像那样:

i // this is already wrong for being single
for i in input_shape //that's correct for cicle
    if i is not None //ugh... so, and what if?

Items that fail the test (that are None here) will be skipped by the loop. 未通过测试的项目(此处为“无”)将被循环跳过。

input_shape = tuple([i for i in input_shape if i is not None])

# Roughly equivalent to    

result = []
for i in input_shape:
    if i is not None:
        result.append(i)
input_shape = tuple(result)

Conceptually the same, except the list comprehension will be faster as the looping is done internally by the interpreter. 在概念上相同,除了列表理解会更快,因为循环是由解释器内部完成的。 Also, it will not leave a result variable around, obviously. 同样,显然不会留下result变量。

the i for i part can be anything, x for x , y for y , or even Kosmos for Kosmos . i for i可以是任何值, x for xy for y ,甚至Kosmos for Kosmos

>>> input_shape = [1, 2, 3]
>>> input_shape = tuple([i for i in input_shape if i is not None])
>>> input_shape
(1, 2, 3)

Here you can see, it converted my list to a tuple by looping over each item. 在这里您可以看到,它通过遍历每个项目将我的列表转换为元组。

Look into a thing called list comprehension, as I am having a tough time explaining it 研究一个叫做列表理解的东西,因为我很难解释它

input_shape = [1,2,3,None,5,1]
print(input_shape)
input_shape = tuple([i for i in input_shape if i is not None])
print(input_shape)

o/p o / p

[1, 2, 3, None, 5, 1]
(1, 2, 3, 5, 1)

as @spectras pointed, Items that fail the test (that are None here) will be skipped by the loop. 如@spectras所指出的,未通过测试的项目(此处为“无”)将被循环跳过。

The following is a list comprehension : 以下是列表理解

[i for i in input_shape if i is not None]

It returns only elements that are not None 它仅返回不为None元素

Then, you call tuple() to convert the resulted list to tuple. 然后,调用tuple()将结果列表转换为元组。

You can achieve the same result by using an ordinary for loop like below: 通过使用如下所示的普通for循环,可以实现相同的结果:

input_shape = [1, 2, None, 'a', 'b', None]
result = []

for i in input_shape:
    if i is not None:
        result.append(i)

print(result)
# Output: [1, 2, 'a', 'b']

Now, we convert result (of type list ) to tuple like this: 现在,我们将result (类型为list )转换为tuple如下所示:

final_result = tuple(result)
print(final_result)
# Output: (1, 2, 'a', 'b')

You were almost there with your separation. 你几乎要分开了。 The inner part (what if) is the expression you noted being wrong ( i ), it actually goes inside. 内部部分(如果)是您指出错误的表达式( i ),实际上它在内部。 And what it does with it is expressed by being inside brackets [] ; 它的作用是用方括号[] it puts those things in a list. 它将这些东西放在列表中。 spectras' answer shows how this works using a variable to hold that list. 光谱的答案显示了如何使用变量保存该列表。 The construct is called a list comprehension . 该构造称为列表理解

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

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