LoginSignup
6
7

More than 5 years have passed since last update.

pythonの三項演算子を配列で置き換える

Last updated at Posted at 2018-12-17

AtCoderの解答を見ていてよく目にするが、なかなか自分でかけないのでメモ。

['b', 'a'][c >= 10]
# ↑は↓と同じ
'a' if c >= 10 else 'b'

c >= 10の部分はTrue/Falseを返す条件文になっていれば良い。
条件文の結果はTrue/Falseになるが、これを配列のインデックスとして用いることで1/0の位置にある値を取り出すことができる。

6
7
3

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