我想将键更改为python字典中的值,但原始字典中的值不唯一。当值不是唯一的时,在python字典中转置值和键
以下是我有:
year_person = {2000: ‘Linda’, 2001: ‘Ron’, 2002: ‘Bruce’, 2003: ‘Linda’, 2004: ‘Bruce’, 2005 ‘Gary’, 2006: ‘Linda’}
这是我想将它更改为:
person_year = {‘Linda’: 2000, ‘Ron’: 2001, ‘Bruce’: 2002, ‘Linda’, 2003: ‘Bruce’, 2004 ‘Gary’, 2005: ‘Linda’: 2006}
当我试图用它来转换一个for循环,我只得到了一个匹配为每个人配对。
你不能那样做。每个密钥必须是唯一的。您可以创建每个键的所有值的列表。 – alan 2012-04-05 22:16:14
rel:http://stackoverflow.com/q/483666/989121 – georg 2012-04-05 22:16:44