Return the dictionary from a list of dictionaries with the highest value in a specific key

天涯浪子 提交于 2021-01-28 04:20:33

问题


I have the following list of dictionaries in python:

data = [{'flag': 'one', 'timestamp': 20190710},
{'flag': 'one', 'timestamp': 20190711},
{'flag': 'two', 'timestamp': 20190712}, 
{'flag': 'two', 'timestamp': 20190709}]

I would like to return the dictionary with the the highest value in the key timestamp and with the value one in the key flag.

I have done something similar here:

Return the key of the maximum value in a dictionary base on criteria in Python

But cannot make it work in this new set-up.


回答1:


You could use max() built-in function with custom key=. Before that, we filter the dictionary only for items with key 'flag' == 'one':

data = [{'flag': 'one', 'timestamp': 20190710},
{'flag': 'one', 'timestamp': 20190711},
{'flag': 'two', 'timestamp': 20190712},
{'flag': 'two', 'timestamp': 20190709}]

print(max((i for i in data if i['flag'] == 'one'), key=lambda k: k['timestamp']))

Prints:

{'flag': 'one', 'timestamp': 20190711}

EDIT: Quick benchmark of what's faster - doing the filtration first or finding the max in whole list:

from random import choice,randint

data = []
for i in range(10000):
    data.append({'flag': choice(['one', 'two']), 'timestamp': randint(1, 1_000_000)})

def fn1():
    return max(data, key=lambda x: (x["flag"] == 'one', x["timestamp"]))

def fn2():
    return max((i for i in data if i['flag'] == 'one'), key=lambda k: k['timestamp'])

from timeit import timeit

t1 = timeit(lambda: fn1(), number=1_000)
t2 = timeit(lambda: fn2(), number=1_000)

print(t1)
print(t2)

Prints:

1.5405012619994523
0.8562619980002637

Which means doing the filtration first is faster.




回答2:


Using max with custom key without iteration.

Ex:

data = [{'flag': 'one', 'timestamp': 20190710},{'flag': 'one', 'timestamp': 20190711},{'flag': 'two', 'timestamp': 20190712}, {'flag': 'two', 'timestamp': 20190709}]
print(max(data, key=lambda x: (x["flag"] == 'one', x["timestamp"])))

Output:

{'flag': 'one', 'timestamp': 20190711}


来源:https://stackoverflow.com/questions/57149702/return-the-dictionary-from-a-list-of-dictionaries-with-the-highest-value-in-a-sp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!