gpt4 book ai didi

python - 如何在 Python 中找到与谓词匹配的序列中的第一个元素?

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:36 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Python: find first element in a sequence that matches a predicate

Python 标准库中是否有封装以下控制流模式的高阶函数?

>>> def find(pred, coll):
... for x in coll:
... if pred(x):
... return x
...
>>> find(lambda n : n % 2 == 0, [3, 5, 8, 9, 6])
8
>>> find(lambda n : n % 2 == 0, [3, 5, 7, 9, 6])
6
>>> find(lambda n : n % 2 == 0, [3, 5, 7, 9, 1])

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com