代码如下:
list = ['aaa','bbb','ccc','ddd'] dict = {key:value for value,key in enumerate(list)} print(dict) # 字典交换key和value reversed_dict = {value:key for key,value in dict.items()} print(reversed_dict)
结果如下:
{'ddd': 3, 'ccc': 2, 'bbb': 1, 'aaa': 0} {0: 'aaa', 1: 'bbb', 2: 'ccc', 3: 'ddd'}