[英]List comprehension with if condition in python
I have a list which has elements of type int and str. 我有一个包含int和str类型的元素的列表。 I need to copy the int elements to another list.
我需要将int元素复制到另一个列表。 I tried a list comprehension which is not working.
我尝试了无法理解的列表理解。 My equivalent loop is working.
我的等效循环正在工作。
input = ['a',1,'b','c',2,3,'c','d']
output = []
[output.append(a) for a in input if type(a) == int]
[None, None, None] [无,无,无]
same logic in loop works. 循环中的相同逻辑。
output = []
for a in input:
if type(a) == int:
output.append(a)
print(output)
[1,2,3,] [1,2,3,]
Can I know what made the difference. 我能知道是什么造成了变化。
When you're doing: 在执行操作时:
input = ['a',1,'b','c',2,3,'c','d']
output = []
[output.append(a) for a in input if type(a) == int]
append
returns None
, so that's why lot of none's append
返回None
,这就是为什么很多都不存在的原因
Btw,: 顺便说一句,:
print(output)
Will be desired result. 将是理想的结果。
Your logic actually works! 您的逻辑实际上有效!
But it's not good to use list comprehension as side-effects, so best way would be: 但是将列表理解作为副作用是不好的,所以最好的方法是:
output=[a for a in input if type(a) == int]
Also final best would be isinstance
: 最后的最佳选择是
isinstance
:
output=[a for a in input if isinstance(a,int)]
An idiomatic solution would be: 惯用的解决方案是:
input = ['a', 1, 'b', 'c', 2, 3, 'c', 'd']
output = [a for a in input if type(a) == int]
You do not want to use output.append. 您不想使用output.append。 That is implied by the list comprehension.
列表理解暗示了这一点。 You can change the first a in the comprehension to an expression containing a.
您可以将理解中的第一个a更改为包含a的表达式。 output.append(a) is a method call that returns NONE
output.append(a)是一个返回NONE的方法调用
Use list comprehensions when you want to collect values into a list you are assigning to a variable or as part of larger expression. 当您想将值收集到要分配给变量的列表中或作为较大表达式的一部分时,请使用列表推导。 Do not use them for side effects as a different format for a 'for loop'.
请勿将它们作为“ for循环”的其他格式用于副作用。 Instead use the for loop.
而是使用for循环。
Spaces after the commas are not required but are considered good style. 逗号后的空格不是必需的,但被认为是好的样式。
input = ['a',1,'b','c',2,3,'c','d']
output = [a for a in input if type(a) == int]
The list comprehension automatically creates the list - no need to use append()
列表理解自动创建列表-无需使用
append()
您可以使用以下解决方案来解决您的问题:
output = [a for a in input if type(a) == int]
A solution would be: 一个解决方案是:
input = ['a',1,'b','c',2,3,'c','d']
output=[item for item in input if type(item) is int] or
output=[item for item in input if isinstance(item,int)]
and the last one is quicker than the first. 最后一个比第一个更快。
but when you use: 但是当您使用时:
input = ['a',1,'b','c',2,3,'c','d']
output=[]
[output.append(a) for a in input if type(a) == int]
it means the code execute two results ,the one is output.append(a) append object in end and the list comprehension with if condition creates a new list object after executing ,as a.append()( Look_in_IDLE ) return None , so there are three None. 这意味着代码执行了两个结果,一个是输出.append(a)最后追加对象,并且如果条件在执行后创建了一个新的列表对象,则列表理解为a.append()( Look_in_IDLE )返回None ,因此是三个无。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.