Find all Key-Elements by the same Value in Dicts

前端 未结 4 505
谎友^
谎友^ 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: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)
    

提交回复
热议问题