mul*_*rse 0 python conditional
以下代码根据a变量所属的范围为v变量分配一个特定的值。
if v>0 and v<1000:
c='green'
elif v>=1000 and v<2000:
c='yellow'
else:
c='red'
Run Code Online (Sandbox Code Playgroud)
效果很好,但是我想知道是否还有更多的Python方式可以编写以下条件块。
0 < v < 1000
就这样 ...
这将适合您的特定用例
var_color = ['green', 'yellow', 'red'][(v >= 1000) + (v >= 2000)]
Run Code Online (Sandbox Code Playgroud)