def filter_list(l):
##newList = []
##for x in l:
## if isinstance(x, int):
## newList.append(x)
##return newList
return [x in l if isinstance(x, int)]
^
Apologies if this is a duplicate, but I didn't see any other syntax error posts that helped. The commented code works, and the uncommented code gives me a syntax error on the last ]. I believe the list comprehension is correct, but I'm not necessarily here to ask that. Why am I getting a syntax error on the ]?
如果这是重复的道歉,但我没有看到任何其他语法错误帖子有帮助。注释的代码有效,未注释的代码在最后一段时间给出了语法错误。我相信列表理解是正确的,但我不一定要问这个。为什么我会在]上遇到语法错误?
3 个解决方案
#1
1
Your list comprehension syntax is incorrect. Perhaps, you meant to use (assuming l
is an iterable):
您的列表推导语法不正确。也许,你打算使用(假设l是可迭代的):
[x for x in l if isinstance(x, int)]
Which is equivalent to:
这相当于:
out = []
for x in l:
if isinstance(x, int):
out.append(x)
#2
1
An easy mistake to make, but stay at it! List Comprehensions are really useful ways to help shorten code. The last line should read:
一个容易犯的错误,但坚持下去!列表理解是帮助缩短代码的有用方法。最后一行应为:
return [x for x in l if isinstance(x, int)]
#3
1
If you already have the explicit loop it's actually easy to convert it to a comprehension.
如果你已经有了显式循环,那么将其转换为理解就很容易了。
So if you have:
所以如果你有:
newList = []
for x in l:
if isinstance(x, int):
newList.append(x)
Then just remove the newlist = []
and put everything inside a [...]
:
然后只需删除newlist = []并将所有内容放在[...]中:
[
for x in l:
if isinstance(x, int):
newList.append(x)
]
The next step is to move the actual element that is being appended at the front and remove the :
下一步是移动前面附加的实际元素并删除:
[
x
for x in l
if isinstance(x, int)
]
That would already work, but generally you also remove the newlines:
这已经可以了,但通常你也会删除换行符:
[x for x in l if isinstance(x, int)]
At least that's how I started "learning" comprehensions, maybe it's also helpful for you.
至少这就是我开始“学习”理解的方式,也许这对你也很有帮助。
The reason why you're code throws the SyntaxError is because it looks like a "ternary" expression that is missing the else
clause. The ternary looks like this:
您的代码抛出SyntaxError的原因是因为它看起来像缺少else子句的“三元”表达式。三元组看起来像这样:
expression1 if condition else expression2
In your case the expression1 is x in l
, the condition is if isinstance(x, int)
and you're missing the else expression2
. And this "ternary" in your case would be wrapped in a list (resulting in a length-one list - if you intended that). For example:
在你的情况下,expression1是l in l,条件是if isinstance(x,int)并且你缺少else expression2。在你的情况下,这个“三元”将被包装在一个列表中(产生一个长度 - 一个列表 - 如果你打算这样做)。例如:
>>> l = [1, 2, 3]
>>> [1 in l if isinstance(1, int) else None]
[True]
#1
1
Your list comprehension syntax is incorrect. Perhaps, you meant to use (assuming l
is an iterable):
您的列表推导语法不正确。也许,你打算使用(假设l是可迭代的):
[x for x in l if isinstance(x, int)]
Which is equivalent to:
这相当于:
out = []
for x in l:
if isinstance(x, int):
out.append(x)
#2
1
An easy mistake to make, but stay at it! List Comprehensions are really useful ways to help shorten code. The last line should read:
一个容易犯的错误,但坚持下去!列表理解是帮助缩短代码的有用方法。最后一行应为:
return [x for x in l if isinstance(x, int)]
#3
1
If you already have the explicit loop it's actually easy to convert it to a comprehension.
如果你已经有了显式循环,那么将其转换为理解就很容易了。
So if you have:
所以如果你有:
newList = []
for x in l:
if isinstance(x, int):
newList.append(x)
Then just remove the newlist = []
and put everything inside a [...]
:
然后只需删除newlist = []并将所有内容放在[...]中:
[
for x in l:
if isinstance(x, int):
newList.append(x)
]
The next step is to move the actual element that is being appended at the front and remove the :
下一步是移动前面附加的实际元素并删除:
[
x
for x in l
if isinstance(x, int)
]
That would already work, but generally you also remove the newlines:
这已经可以了,但通常你也会删除换行符:
[x for x in l if isinstance(x, int)]
At least that's how I started "learning" comprehensions, maybe it's also helpful for you.
至少这就是我开始“学习”理解的方式,也许这对你也很有帮助。
The reason why you're code throws the SyntaxError is because it looks like a "ternary" expression that is missing the else
clause. The ternary looks like this:
您的代码抛出SyntaxError的原因是因为它看起来像缺少else子句的“三元”表达式。三元组看起来像这样:
expression1 if condition else expression2
In your case the expression1 is x in l
, the condition is if isinstance(x, int)
and you're missing the else expression2
. And this "ternary" in your case would be wrapped in a list (resulting in a length-one list - if you intended that). For example:
在你的情况下,expression1是l in l,条件是if isinstance(x,int)并且你缺少else expression2。在你的情况下,这个“三元”将被包装在一个列表中(产生一个长度 - 一个列表 - 如果你打算这样做)。例如:
>>> l = [1, 2, 3]
>>> [1 in l if isinstance(1, int) else None]
[True]