如何在Python中反转字典键值对的顺序?例如,我有这本词典:
english_spanish = {"hi": "hola", "thanks": "gracias", "yes": "si", "no": "no"}
我想要反转它以便它返回:
english_spanish = {"hola": "hi", "gracias": "thanks", "si": "yes", "no": "no"}
我想知道如何使用lampda
来做这件事,但我似乎不明白它是如何运作的
答案 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'}