要查找seq
与匹配的序列中的第一个元素predicate
:
next(x for x in seq if predicate(x))
或(itertools.ifilter
在Python 2上):
next(filter(predicate, seq))
StopIteration
如果没有,它将引发。
None
如果没有这样的元素,则返回:
next((x for x in seq if predicate(x)), None)
要么:
next(filter(predicate, seq), None)