How do Python's any and all functions work?

后端 未结 8 1697
挽巷
挽巷 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条回答
  •  半阙折子戏
    2020-11-22 01:48

    The code in question you're asking about comes from my answer given here. It was intended to solve the problem of comparing multiple bit arrays - i.e. collections of 1 and 0.

    any and all are useful when you can rely on the "truthiness" of values - i.e. their value in a boolean context. 1 is True and 0 is False, a convenience which that answer leveraged. 5 happens to also be True, so when you mix that into your possible inputs... well. Doesn't work.

    You could instead do something like this:

    [len(set(x)) > 1 for x in zip(*d['Drd2'])]
    

    It lacks the aesthetics of the previous answer (I really liked the look of any(x) and not all(x)), but it gets the job done.

提交回复
热议问题