How to uniqufy the tuple element?

前端 未结 4 1733
星月不相逢
星月不相逢 2021-01-19 06:48

i have a result tuple of dictionaries.

result = ({\'name\': \'xxx\', \'score\': 120L }, {\'name\': \'xxx\', \'score\': 100L}, {\'name\': \'yyy\', \'score\':          


        
4条回答
  •  面向向阳花
    2021-01-19 07:16

    How about...

    inp  = ({'name': 'xxx', 'score': 120L }, {'name': 'xxx', 'score': 100L}, {'name': 'yyy', 'score': 10L})
    
    temp = {}
    for dct in inp:
        if dct['score'] > temp.get(dct['name']): temp[dct['name']] = dct['score']
    
    result = tuple({'name': name, 'score': score} for name, score in temp.iteritems())
    

提交回复
热议问题