Sequence find function in Python

后端 未结 4 389
生来不讨喜
生来不讨喜 2020-12-15 04:20

How do I find an object in a sequence satisfying a particular criterion?

List comprehension and filter go through the entire list. Is the only alternative a handmade

4条回答
  •  时光说笑
    2020-12-15 05:00

    Too lazy to write:

    mylist = [10, 2, 20, 5, 50]
    max(mylist, key=lambda x: x>10)
    

提交回复
热议问题