How can a first-match lookup represent an empty result?

next(iterator, default) supplies a caller-defined fallback on exhaustion, avoiding materializing all candidates first.

Python example and interpretation

Neither candidate clears 0.9, so the lookup returns None. Use a distinct sentinel if None is also a legitimate candidate value.

next((x for x in [0.2,0.8] if x>=0.9),None)

Result None

中文

从候选流选择首个合格样本,怎样表达没有命中?

next(iterator,default) 在迭代耗尽时返回调用者指定的默认值,无需先把候选全部装入内存。

Python 算例与适用条件

阈值为 0.9 时两项分数都不够,得到 None。如果合法样本本身也能为 None,应选用独立哨兵区分两种结果。

next((x for x in [0.2,0.8] if x>=0.9),None)

结果 None

References / 参考来源

Keep reading / 继续阅读