Why is there no first(iterable) built-in function in Python?(为什么 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
这篇关于为什么 Python 中没有 first(iterable) 内置函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 Python 中没有 first(iterable) 内置函数?


基础教程推荐
- 如何在Python中绘制多元函数? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01