它测试数值比较
我将创建一个标记dict,id作为key,sublist作为value。循环输入,如果值更高,则覆盖dict条目(不要忘记转换为整数)
my_list = [
{'score': '192', 'imageId': '6184de26-e11d-4a7e-9c44-a1af8012d8d0', 'label': 'Door'},
{'score': '61', 'imageId': 'fffffe26-e11d-4a7e-9c44-a1af8012d8d0', 'label': 'misc'},
{'score': '761', 'imageId': '6184de26-e11d-4a7e-9c44-a1af8012d8d0', 'label': 'Sliding Door'},
{'score': '45', 'imageId': '6184de26-e11d-4a7e-9c44-a1af8012d8d0', 'label': 'Door'},
]
import collections
d = dict()
for subdict in my_list:
score = int(subdict['score'])
image_id = subdict['imageId']
if image_id not in d or int(d[image_id]['score']) < score:
d[image_id] = subdict
new_list = list(d.values())
[{'imageId': 'fffffe26-e11d-4a7e-9c44-a1af8012d8d0',
'label': 'misc',
'score': '61'},
{'imageId': '6184de26-e11d-4a7e-9c44-a1af8012d8d0',
'label': 'Sliding Door',
'score': '761'}]