擅长:python、mysql、java
<p>我建议您稍微加强示例,以便:</p>
<ul>
<li>它测试数值比较</li>
<li>它有不连续的“重复”元素</li>
</ul>
<p>我将创建一个标记dict,id作为键,sublist作为值。循环输入,如果值更高,则覆盖dict条目(不要忘记强制转换为整数)</p>
<pre><code>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())
</code></pre>
<p>结果(使用词典时顺序可能会改变):</p>
<pre><code>[{'imageId': 'fffffe26-e11d-4a7e-9c44-a1af8012d8d0',
'label': 'misc',
'score': '61'},
{'imageId': '6184de26-e11d-4a7e-9c44-a1af8012d8d0',
'label': 'Sliding Door',
'score': '761'}]
</code></pre>