How to reverse a dictionary that has repeated values

后端 未结 6 1788
抹茶落季
抹茶落季 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)
    
    0 讨论(0)
  • 2020-11-28 15:07

    Using collections.defaultdict:

    from collections import defaultdict
    
    reversed_dict = defaultdict(list)
    for key, value in mydict.items():
        reversed_dict[value].append(key)
    
    0 讨论(0)
  • 2020-11-28 15:07
    reversed_dict = {}
    for key, value in mydict.items():
        reversed_dict.setdefault(value, [])
        reversed_dict[value].append(key)
    
    0 讨论(0)
  • 2020-11-28 15:14
    for k,v in dict.iteritems():
        try:
          reversed_dict[v].append(k)
        except KeyError:
           reversed_dict[v]=[k]
    
    0 讨论(0)
  • 2020-11-28 15:14

    I think you're wasting a few cycles by replacing a key with the same key again and again...

    reversed_dict = {}
    for value in mydict.values():
        if value not in reversed_dict.keys(): #checking to be sure it hasn't been done.
            reversed_dict[value] = []
            for key in mydict.keys():
                if mydict[key] == value:
                    if key not in reversed_dict[value]: reversed_dict[value].append(key)
    
    0 讨论(0)
  • 2020-11-28 15:14

    Using itertools.groupby:

    from operator import itemgetter
    from itertools import groupby
    
    snd = itemgetter(1)
    
    def sort_and_group(itr, f):
        return groupby(sorted(itr, key=f), f)
    
    mydict =  {'a': 1, 'c': 2, 'b': 1, 'e': 2, 'd': 3, 'h': 1, 'j': 3}
    reversed_dict = {number: [char for char,_ in v] 
                     for number, v in sort_and_group(mydict.items(), snd)}
    
    0 讨论(0)
提交回复
热议问题