如果我有这本词典,我该怎么做才能获得键而不是读取单词。 例如,如果我写2 22 1响应必须是A B。
compare functions
我正在尝试使用此循环,但出现了错误:
value= {"A": 2, "B": 22, "C": 222, "D": 3, "E": 33,"F":333, "G": 4, "H": 44, "I": 444, "J": 5, "K": 55, "L": 555, "M": 6, "N": 66, "O": 666, "P": 7, "Q": 77, "R": 777, "S": 7777, "T": 8, "U": 88, "V": 888, "W": 9, "X": 99, "Y": 999, "Z": 9999}
答案 0 :(得分:0)
您可以撤消字典:
value= {"A": 2, "B": 22, "C": 222, "D": 3, "E": 33,"F":333, "G": 4, "H": 44, "I": 444, "J": 5, "K": 55, "L": 555, "M": 6, "N": 66, "O": 666, "P": 7, "Q": 77, "R": 777, "S": 7777, "T": 8, "U": 88, "V": 888, "W": 9, "X": 99, "Y": 999, "Z": 9999}
new_value = {b:a for a, b in value.items()}
print(new_value[2])
输出:
A
关于您的最新评论,您可以尝试:
final_results = [a for a, b in value.items() if b == 22]