删除列表重复元素 python_在Python中删除列表列表中的重复元素

它测试数值比较

我将创建一个标记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'}]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值