使用" lambda"反转字典中键值的顺序

时间:2017-10-10 13:23:58

标签: python

如何在Python中反转字典键值对的顺序?例如,我有这本词典:

english_spanish = {"hi": "hola", "thanks": "gracias", "yes": "si", "no": "no"}

我想要反转它以便它返回:

english_spanish = {"hola": "hi", "gracias": "thanks", "si": "yes", "no": "no"}

我想知道如何使用lampda来做这件事,但我似乎不明白它是如何运作的

1 个答案:

答案 0 :(得分:-1)

english_spanish = {"hi": "hola", "thanks": "gracias", "yes": "si", "no": "no"}
spanish_english = {v:k for (k, v) in english_spanish.items()}
print(spanish_english)
# {'hola': 'hi', 'no': 'no', 'gracias': 'thanks', 'si': 'yes'}