以下代码的最佳单行替换是什么?我确信有更聪明的方法。
choices = ((1, 'ONE'), (2, 'TWO'), (3, 'THREE'))
some_int = 2
for choice in choices:
if choice[0] == some_int:
label = choice[1]
break;
# label == 'TWO'
最佳答案
labels = dict(choices)
label = labels[some_int]
当然,如果您在其他任何地方都不需要标签
,您可以将其加入一行。
关于python - 高效搜索二元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2104095/