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) 内置函数?
基础教程推荐
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
