[英]Why does this generator expression raise a syntax error?
This line throws an error saying I didn't define x
even though I stated that x
is an element of the list: 此行引发错误,即使我声明x
是列表的元素,我也没有定义x
:
any(i.isdigit() for i in x for x in [name.id for name in all.names])
So x
is a string element of the list, and I am checking if for some character in each element x
, that element x
contains a number using .isdigit()
. 因此, x
是列表中的字符串元素,我检查如果在每一个元素一些字符x
,该元素x
使用含有许多.isdigit()
How come this doesn't work? 为什么这不起作用?
Comprehensions/generator expressions in Python nest from left to right (yeah, it can be a bit confusing). Python中的推导/生成器表达式从左到右嵌套(是的,可能有点令人困惑)。 Swap them: 交换它们:
for x in [name.id for name in all.names] for i in x
Or separate out for clarity: 或为清楚起见分开:
def contains_digit(s):
return any(c.isdigit() for c in s)
any(contains_digit(name.id) for name in all.names)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.