How to reverse a dictionary that has repeated values

后端 未结 6 1811
抹茶落季
抹茶落季 2020-11-28 14:48

I have a dictionary with almost 100,000 (key, value) pairs and the majority of the keys map to the same values. For example:

mydict =  {\'a\': 1, \'c\': 2, \'         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 15:03

    reversed_dict = collections.defaultdict(list)
    for key, value in dict_.iteritems():
      reversed_dict[value].append(key)
    

提交回复
热议问题