Default value for next element in Python iterator if iterator is empty?(如果迭代器为空,Python迭代器中下一个元素的默认值?)
问题描述
我有一个对象列表,我想找到给定方法为某个输入值返回 true 的第一个对象.这在 Python 中相对容易做到:
I have a list of objects, and I would like to find the first one for which a given method returns true for some input value. This is relatively easy to do in Python:
pattern = next(p for p in pattern_list if p.method(input))
但是,在我的应用程序中,通常没有 p.method(input)
为真的这样的 p
,因此这将引发 StopIteration
异常.有没有一种不写 try/catch 块的惯用方法来处理这个问题?
However, in my application it is common that there is no such p
for which p.method(input)
is true, and so this will raise a StopIteration
exception. Is there an idiomatic way to handle this without writing a try/catch block?
特别是,用 if pattern is not None
条件来处理这种情况似乎会更干净,所以我想知道是否有办法扩展我对 的定义code>pattern
在迭代器为空时提供 None
值——或者如果有更 Pythonic 的方式来处理整个问题!
In particular, it seems like it would be cleaner to handle that case with something like an if pattern is not None
conditional, so I'm wondering if there's a way to expand my definition of pattern
to provide a None
value when the iterator is empty -- or if there's a more Pythonic way to handle the overall problem!
推荐答案
next
接受默认值:
next
accepts a default value:
next(...)
next(iterator[, default])
Return the next item from the iterator. If default is given and the iterator
is exhausted, it is returned instead of raising StopIteration.
等等
>>> print next(i for i in range(10) if i**2 == 9)
3
>>> print next(i for i in range(10) if i**2 == 17)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> print next((i for i in range(10) if i**2 == 17), None)
None
请注意,出于语法原因,您必须将 genexp 包含在额外的括号中,否则:
Note that you have to wrap the genexp in the extra parentheses for syntactic reasons, otherwise:
>>> print next(i for i in range(10) if i**2 == 17, None)
File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole argument
这篇关于如果迭代器为空,Python迭代器中下一个元素的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果迭代器为空,Python迭代器中下一个元素的默


- pytorch 中的自适应池是如何工作的? 2022-07-12
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 沿轴计算直方图 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01