Python equivalent of LINQ All function?

前端 未结 1 1061
被撕碎了的回忆
被撕碎了的回忆 2021-02-12 19:20

What is the idiomatic Python way to test if all elements in a collection satisfy a condition? (The .NET All() method fills this niche nicely in C#.)

There\'s the obvious

相关标签:
1条回答
  • 2021-02-12 20:00
    all_match = all(test(x) for x in stuff)
    

    This short-circuits and doesn't require stuff to be a list -- anything iterable will work -- so has several nice features.

    There's also the analogous

    any_match = any(test(x) for x in stuff)
    
    0 讨论(0)
提交回复
热议问题