可能重复: Python:使用字典中的值交换密钥的最佳方法?
假设我需要在字典中交换键的值.
这就是我的想法(假设值的值是唯一的):
>>> my_dict = {'x':1, 'y':2, 'z':3} >>> my_dict2 = {} >>> for key, val in my_dict.items(): my_dict2[val] = key
还有其他有效的方法吗?
python python-3.x
python ×1
python-3.x ×1