为什么 Python 中没有 first(iterable) 内置函数?
问题描述
我想知道 Python 内置函数中没有 first(iterable)
是否有原因,有点类似于 any(iterable)
和 all(iterable)
(它可能藏在某个 stdlib 模块中,但我在 itertools
中看不到它).first
将执行短路生成器评估,从而可以避免不必要的(并且可能是无限数量的)操作;即
I'm wondering if there's a reason that there's no first(iterable)
in the Python built-in functions, somewhat similar to any(iterable)
and all(iterable)
(it may be tucked in a stdlib module somewhere, but I don't see it in itertools
). first
would perform a short-circuit generator evaluation so that unnecessary (and a potentially infinite number of) operations can be avoided; i.e.
def identity(item):
return item
def first(iterable, predicate=identity):
for item in iterable:
if predicate(item):
return item
raise ValueError('No satisfactory value found')
这样你可以表达如下内容:
This way you can express things like:
denominators = (2, 3, 4, 5)
lcd = first(i for i in itertools.count(1)
if all(i % denominators == 0 for denominator in denominators))
显然你不能在这种情况下执行 list(generator)[0]
,因为生成器不会终止.
Clearly you can't do list(generator)[0]
in that case, since the generator doesn't terminate.
或者,如果您有一堆正则表达式要匹配(当它们都具有相同的 groupdict
接口时很有用):
Or if you have a bunch of regexes to match against (useful when they all have the same groupdict
interface):
match = first(regex.match(big_text) for regex in regexes)
通过避免 list(generator)[0]
和在正匹配时短路,您可以节省大量不必要的处理.
You save a lot of unnecessary processing by avoiding list(generator)[0]
and short-circuiting on a positive match.
解决方案
如果你有一个迭代器,你可以调用它的 next
方法.比如:
If you have an iterator, you can just call its next
method. Something like:
In [3]: (5*x for x in xrange(2,4)).next()
Out[3]: 10
相关文章