How do Python's any and all functions work?

后端 未结 8 1544
挽巷
挽巷 2020-11-22 01:13

I\'m trying to understand how the any() and all() Python built-in functions work.

I\'m trying to compare the tuples so that if any value i

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 01:50

    The concept is simple:

    M =[(1, 1), (5, 6), (0, 0)]
    
    1) print([any(x) for x in M])
    [True, True, False] #only the last tuple does not have any true element
    
    2) print([all(x) for x in M])
    [True, True, False] #all elements of the last tuple are not true
    
    3) print([not all(x) for x in M])
    [False, False, True] #NOT operator applied to 2)
    
    4) print([any(x)  and not all(x) for x in M])
    [False, False, False] #AND operator applied to 1) and 3)
    # if we had M =[(1, 1), (5, 6), (1, 0)], we could get [False, False, True]  in 4)
    # because the last tuple satisfies both conditions: any of its elements is TRUE 
    #and not all elements are TRUE 
    

提交回复
热议问题