【发布时间】:2021-03-02 21:07:19
【问题描述】:
我已经用 Python 编写了以下代码,但编码风格看起来很糟糕,我想用更好的东西替换它:
if A == 0 and B == 0:
color = 'red'
elif A == 1 and B == 1:
color = 'yellow'
elif A == 2 and B == 2:
color = 'blue'
elif A == 0 and B == 1:
color = 'orange'
elif A == 1 and B == 0:
color = 'orange'
elif A == 2 and B == 1:
color = 'green'
elif A == 1 and B == 2:
color = 'green'
elif A == 0 and B == 2:
color = 'purple'
elif A == 2 and B == 0:
color = 'purple'
我建议使用我在下面写的字典,但我很难找到如何在 Python 中编写代码,因为每个键都有多个值。
color_dict = {
"red": [[0,0]],
"orange": [[0,1], [1,0]],
"yellow": [[1,1]],
"green": [[1,2],[2,1]],
"blue": [[2,2]],
"purple": [[2,0],[0,2]]
【问题讨论】:
标签: python dictionary if-statement