End*_*ook 0 python conditional-operator
我最近发现了这个问题:Does Python have a ternary conditional operator? 并发现称为三元条件的东西a = b if c == d else e。
我的问题:有一种方法可以使三元条件与elif? 类似的东西a = b if c == d elif e == f i else j。
您可以链接条件运算符,但不建议这样做,因为它很难阅读。关联性以您期望的方式工作(就像除 PHP 之外的所有语言一样):
a = b if c == d else i if e == f else j
Run Code Online (Sandbox Code Playgroud)
这在英语中的意思是“如果 c 等于 d,则将 b 分配给 a,否则,如果 e 等于 f,则分配 i,否则分配 j”。