需要了解Python生成器对象
-
12-12-2019 - |
题
如下:
name = 'TODD'
chars = set('AEIOU')
for ii in range(-1, int(math.copysign(len(name) + 1, -1)), -1):
if any((cc in chars) for cc in name[ii]):
print 'Found'
else:
print 'Not Found'
.
我明白任何(...)内部是一个生成器对象。我不明白的是缺乏括号 - 如果括号属于任何()函数,则不会在发电机表达式周围有另一组括号?
谢谢。
其他提示
否,不需要额外的假钉,实际上也是Parens始终需要“布尔表达”您正在测试的“布尔表达”,见这两个简单的例子:
In [37]: any(i > 10 for i in range(19))
Out[37]: True
In [38]: all(i > 10 for i in range(19))
Out[38]: False
.
您拥有的是一个参数的函数调用(您的生成器表达式),因此没有必需的伪费。请参阅生成器表达式Docs 更多信息。
For function calls with just one argument (that generator expression), they aren't required per the docs
The parentheses can be omitted on calls with only one argument. See section Calls for the detail.
不隶属于 StackOverflow