any() function in Python with a callback

前端 未结 8 1118
别那么骄傲
别那么骄傲 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.

    0 讨论(0)
  • 2020-12-13 02:20

    How about:

    >>> any(isinstance(e, int) and e > 0 for e in [1,2,'joe'])
    True
    

    It also works with all() of course:

    >>> all(isinstance(e, int) and e > 0 for e in [1,2,'joe'])
    False
    
    0 讨论(0)
提交回复
热议问题