LoginSignup
2
2

More than 5 years have passed since last update.

リストのすべての要素が特定の条件にマッチするか判定する

Last updated at Posted at 2018-02-10
qiita.py
lis=[2,4,40]
print(all(elem %2==0 for elem in lis))
#>>>True
print(all(elem //2<=3 for elem in lis))
#>>>False

... for ... in ... のような表記は、[] で囲んでリスト内包表記として使う他に、 [] で囲まずにジェネレータ式というものにして all の引数に直接渡す使い方もあるらしい。 all にはリスト内包表記よりもジェネレータ式を渡したほうが速い。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2