3 回答
TA贡献1785条经验 获得超8个赞
假设list1已经按T您可以使用排序itertools.groupby。
from itertools import groupby
li = [
{ 'T': 1234, 'V': 10, 'O': 1 },
{ 'T': 2345, 'V': 50, 'O': 5 },
{ 'T': 2345, 'V': 30, 'O': 3 },
{ 'T': 3456, 'V': 40, 'O': 91 },
]
output = [max(group, key=lambda d: d['V'])
for _, group in groupby(li, key=lambda d: d['T'])]
print(output)
# [{'T': 1234, 'V': 10, 'O': 1}, {'T': 2345, 'V': 50, 'O': 5}, {'T': 3456, 'V': 40, 'O': 91}]
如果不是,groupby仍然可以使用 withsort以实现 O(nlogn) 解决方案
order_by_t = lambda d: d['T']
li.sort(key=order_by_t)
output = [max(group, key=lambda d: d['V'])
for _, group in groupby(li, key=order_by_t)]
TA贡献1890条经验 获得超9个赞
假设您的列表已按 排序T,您可以简单地跟踪V一次传递中的最大元素,并在找到时替换最大值:
list1 = [
{ 'T': 1234, 'V': 10, 'O': 1 },
{ 'T': 2345, 'V': 50, 'O': 5 },
{ 'T': 2345, 'V': 30, 'O': 3 },
{ 'T': 3456, 'V': 40, 'O': 91 },
]
unique = {}
for dic in list1:
key = dic['T']
found = unique.get(key)
# If value found and doesn't exceed current maximum, just ignore
if found and dic['V'] <= found['V']:
continue
# otherwise just update normally
unique[key] = dic
print(list(unique.values()))
# [{'T': 1234, 'V': 10, 'O': 1}, {'T': 2345, 'V': 50, 'O': 5}, {'T': 3456, 'V': 40, 'O': 91}]
如果您的列表不能保证按 排序T,您可以预先应用排序 withT作为排序key:
from operator import itemgetter
sorted(list1, key=itemgetter('T'))
operator.itemgetter以上使用与使用相同:
sorted(list1, key=lambda x: x['T'])
TA贡献1810条经验 获得超4个赞
这是循序渐进的方法。它迭代您的列表一次并构建一个新列表:
list1 = [
{ 'T': 1234, 'V': 10, 'O': 1 },
{ 'T': 2345, 'V': 50, 'O': 5 },
{ 'T': 2345, 'V': 30, 'O': 3 },
{ 'T': 3456, 'V': 40, 'O': 91 },
]
# add this step if not already sorted by T
# list1 = sorted(list1, key = lambda x: x["T"])
list2 = []
for e in list1:
t, v, o = e["T"], e["V"], e["O"]
# we already stored something and same T
if list2 and list2[-1]["T"] == t:
# smaller V ?
if list2[-1]["V"] < v:
# overwrite dict elements
list2[-1]["V"] = v
list2[-1]["O"] = o
# did not store anything or other T
else:
list2.append(e)
print(list2)
输出:
[{'T': 1234, 'O': 1, 'V': 10},
{'T': 2345, 'O': 5, 'V': 50},
{'T': 3456, 'O': 91, 'V': 40}]
添加回答
举报