any() function in Python with a callback

前端 未结 8 1148
别那么骄傲
别那么骄傲 2020-12-13 01:34

The Python standard library defines an any() function that

Return True if any element of the iterable is true. If the iterable is empty, return False.

8条回答
  •  难免孤独
    2020-12-13 02:16

    You can use a combination of any and map if you really want to keep your lambda notation like so :

    any(map(lambda e: isinstance(e, int) and e > 0, [1, 2, 'joe']))
    

    But it is better to use a generator expression because it will not build the whole list twice.

提交回复
热议问题