Find all Key-Elements by the same Value in Dicts

前端 未结 4 503
谎友^
谎友^ 2020-12-06 06:02

I have question about Dictionaries in Python.

here it is:

I have a dict like dict = { \'abc\':\'a\', \'cdf\':\'b\', \'gh\':\'a\', \'fh\':\'g\', \'hfz\'

相关标签:
4条回答
  • 2020-12-06 06:34

    If you do specifically want tuples as the values in your new dictionary, you can still use defaultdict, and use tuple concatenation. This solution works in Python 3.4+:

    from collections import defaultdict
    
    source = {'abc': 'a', 'cdf': 'b', 'gh': 'a', 'fh': 'g', 'hfz': 'g'}
    target = defaultdict(tuple)
    
    for key in source:
        target[source[key]] += (key, )
    
    print(target)
    

    Which will produce

    defaultdict(<class 'tuple'>, {'a': ('abc', 'gh'), 'g': ('fh', 'hfz'), 'b': ('cdf',)})
    

    This will probably be slower than generating a dictionary by list insertion, and will create more objects to be collected. So, you can build your dictionary out of lists, and then map it into tuples:

    target2 = defaultdict(list)
    
    for key in source:
        target2[source[key]].append(key)
    
    for key in target2:
        target2[key] = tuple(target2[key])
    
    print(target2)
    

    Which will give the same result as above.

    0 讨论(0)
  • 2020-12-06 06:44

    Here's a naive implementation. Someone with better Python skills can probably make it more concise and awesome.

    dict = { 'abc':'a', 'cdf':'b', 'gh':'a', 'fh':'g', 'hfz':'g' }
    
    new_dict = {}
    for pair in dict.items():
        if pair[1] not in new_dict.keys():
            new_dict[pair[1]] = []
    
        new_dict[pair[1]].append(pair[0])
    
    print new_dict
    

    This produces

    {'a': ['abc', 'gh'], 'b': ['cdf'], 'g': ['fh', 'hfz']}
    
    0 讨论(0)
  • 2020-12-06 06:50

    If you are fine with lists instead of tuples in the new dictionary, you can use

    from collections import defaultdict
    some_dict = { 'abc':'a', 'cdf':'b', 'gh':'a', 'fh':'g', 'hfz':'g' }
    new_dict = defaultdict(list)
    for k, v in some_dict.iteritems():
        new_dict[v].append(k)
    

    If you want to avoid the use of defaultdict, you could also do

    new_dict = {}
    for k, v in some_dict.iteritems():
        new_dict.setdefault(v, []).append(k)
    
    0 讨论(0)
  • 2020-12-06 06:50

    It can be done this way too, without using any extra functions .

    some_dict = { 'abc':'a', 'cdf':'b', 'gh':'a', 'fh':'g', 'hfz':'g' }
    new_dict = { }
    for keys in some_dict:
        new_dict[some_dict[keys]] = [ ]
    for keys in some_dict:
        new_dict[some_dict[keys]].append(keys)
        
    print(new_dict)
    
    0 讨论(0)
提交回复
热议问题