python list of dictionaries find duplicates based on value

后端 未结 3 1664
囚心锁ツ
囚心锁ツ 2020-12-10 07:34

I have a list of dicts in python 2.7.

a =[{\'id\': 1,\'desc\': \'smth\'},
    {\'id\': 2,\'desc\': \'smthelse\'},
    {\'id\': 1,\'desc\': \'smthelse2\'},
          


        
3条回答
  •  生来不讨喜
    2020-12-10 07:59

    It is better to keep the "desc" values as lists everywhere even if they contain a single element only. This way you can do

    for d in b:
        print d['id']
        for desc in d['desc']:
            print desc
    

    This would work for strings too, just returning individual characters, which is not what you want.

    And now the solution giving you a list of dicts of lists:

    a =[{'id': 1,'desc': 'smth'},{'id': 2,'desc': 'smthelse'},{'id': 1,'desc': 'smthelse2'},{'id': 1,'desc': 'smthelse3'}]
    
    c = {}
    for d in a:
        c.setdefault(d['id'], []).append(d['desc'])
    b = [{'id': k, 'desc': v} for k,v in c.iteritems()]
    

    b is now:

    [{'desc': ['smth', 'smthelse2', 'smthelse3'], 'id': 1},
     {'desc': ['smthelse'], 'id': 2}]
    

提交回复
热议问题