如何在字典中交换键的值

时间:2009-07-06 15:41:45

标签: python python-3.x

  

可能重复:
  Python: Best Way to Exchange Keys with Values in a Dictionary?

假设我需要在字典中交换键值。

这就是我的想法(假设值的值是唯一的):

>>> my_dict = {'x':1, 'y':2, 'z':3}
>>> my_dict2 = {}
>>> for key, val in my_dict.items():
        my_dict2[val] = key

还有其他有效的方法吗?

0 个答案:

没有答案