如何从字典列表中按label提取对应width最大值的唯一项
问题:为每个label找到对应width值最大的项
输入
[{'height': 32, 'label': '1', 'left': 944, 'top': 173, 'width': 17}, {'height': 60, 'label': '1', 'left': 617, 'top': 276, 'width': 19}, {'height': 43, 'label': '1', 'left': 508, 'top': 296, 'width': 19}, {'height': 91, 'label': '3', 'left': 247, 'top': 194, 'width': 20}, {'height': 94, 'label': '1', 'left': 114, 'top': 193, 'width': 22}, {'height': 96, 'label': '4', 'left': 281, 'top': 185, 'width': 22}, {'height': 51, 'label': '2', 'left': 486, 'top': 288, 'width': 23}, {'height': 67, 'label': '2', 'left': 611, 'top': 142, 'width': 25}, {'height': 42, 'label': '2', 'left': 919, 'top': 164, 'width': 25}, {'height': 48, 'label': '3', 'left': 829, 'top': 165, 'width': 25}, {'height': 73, 'label': '3', 'left': 363, 'top': 699, 'width': 25}, {'height': 91, 'label': '1', 'left': 133, 'top': 192, 'width': 26}, {'height': 95, 'label': '2', 'left': 102, 'top': 447, 'width': 26}, {'height': 70, 'label': '5', 'left': 258, 'top': 610, 'width': 26}, {'height': 97, 'label': '6', 'left': 164, 'top': 317, 'width': 27}, {'height': 55, 'label': '4', 'left': 588, 'top': 283, 'width': 28}, {'height': 65, 'label': '4', 'left': 379, 'top': 401, 'width': 28}, {'height': 91, 'label': '4', 'left': 157, 'top': 193, 'width': 29}, {'height': 96, 'label': '4', 'left': 186, 'top': 190, 'width': 29}, {'height': 74, 'label': '4', 'left': 248, 'top': 339, 'width': 29}, {'height': 60, 'label': '3', 'left': 422, 'top': 281, 'width': 29}, {'height': 98, 'label': '1', 'left': 157, 'top': 445, 'width': 29}, {'height': 106, 'label': '2', 'left': 211, 'top': 440, 'width': 29}, {'height': 68, 'label': '6', 'left': 414, 'top': 398, 'width': 29}]
预期输出
[{'height': 98, 'label': '1', 'left': 157, 'top': 445, 'width': 29}, {'height': 106, 'label': '2', 'left': 211, 'top': 440, 'width': 29}, {'height': 60, 'label': '3', 'left': 422, 'top': 281, 'width': 29}, {'height': 91, 'label': '4', 'left': 157, 'top': 193, 'width': 29}, {'height': 70, 'label': '5', 'left': 258, 'top': 610, 'width': 26}, {'height': 68, 'label': '6', 'left': 414, 'top': 398, 'width': 29}]
解决方案
可以通过Python字典分组的方式实现,遍历列表时为每个label保留width最大的项:
# 输入列表 items = [ {'height': 32, 'label': '1', 'left': 944, 'top': 173, 'width': 17}, {'height': 60, 'label': '1', 'left': 617, 'top': 276, 'width': 19}, {'height': 43, 'label': '1', 'left': 508, 'top': 296, 'width': 19}, {'height': 91, 'label': '3', 'left': 247, 'top': 194, 'width': 20}, {'height': 94, 'label': '1', 'left': 114, 'top': 193, 'width': 22}, {'height': 96, 'label': '4', 'left': 281, 'top': 185, 'width': 22}, {'height': 51, 'label': '2', 'left': 486, 'top': 288, 'width': 23}, {'height': 67, 'label': '2', 'left': 611, 'top': 142, 'width': 25}, {'height': 42, 'label': '2', 'left': 919, 'top': 164, 'width': 25}, {'height': 48, 'label': '3', 'left': 829, 'top': 165, 'width': 25}, {'height': 73, 'label': '3', 'left': 363, 'top': 699, 'width': 25}, {'height': 91, 'label': '1', 'left': 133, 'top': 192, 'width': 26}, {'height': 95, 'label': '2', 'left': 102, 'top': 447, 'width': 26}, {'height': 70, 'label': '5', 'left': 258, 'top': 610, 'width': 26}, {'height': 97, 'label': '6', 'left': 164, 'top': 317, 'width': 27}, {'height': 55, 'label': '4', 'left': 588, 'top': 283, 'width': 28}, {'height': 65, 'label': '4', 'left': 379, 'top': 401, 'width': 28}, {'height': 91, 'label': '4', 'left': 157, 'top': 193, 'width': 29}, {'height': 96, 'label': '4', 'left': 186, 'top': 190, 'width': 29}, {'height': 74, 'label': '4', 'left': 248, 'top': 339, 'width': 29}, {'height': 60, 'label': '3', 'left': 422, 'top': 281, 'width': 29}, {'height': 98, 'label': '1', 'left': 157, 'top': 445, 'width': 29}, {'height': 106, 'label': '2', 'left': 211, 'top': 440, 'width': 29}, {'height': 68, 'label': '6', 'left': 414, 'top': 398, 'width': 29} ] # 用字典存储每个label对应的最大width项 max_width_items = {} for item in items: label = item['label'] # 如果label不在字典中,或者当前项width更大,则更新 if label not in max_width_items or item['width'] > max_width_items[label]['width']: max_width_items[label] = item # 转成列表并按label排序,和预期输出顺序一致 result = sorted(max_width_items.values(), key=lambda x: x['label']) print(result)
逻辑说明
- 初始化空字典
max_width_items,键为label,值为对应width最大的字典项。 - 遍历输入列表中的每个项,对于每个
label:- 若该
label未在字典中,直接将当前项存入字典。 - 若已存在,则比较当前项的
width与字典中对应项的width,保留更大的那个。
- 若该
- 将字典的值转换为列表,并按
label排序,得到和预期一致的结果。
内容的提问来源于stack exchange,提问作者milas86534
相关产品推荐
相关产品推荐

