LoginSignup
10
7

More than 5 years have passed since last update.

Pythonにおけるif文の長いor列挙の回避方法

Last updated at Posted at 2018-10-11
# ifのor列挙は長い
if flag=="apple" or flag=="banana" or flag=="donuts" or flag=="eggnog":
    pass

# リストのin演算子を使えば少しスマート
if flag in ["apple", "banana", "donuts", "eggnog"]:
    pass

追記

速度を比較しました
or列挙、リスト、タプル、集合でのif文の速度評価

10
7
2

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
10
7