gpt4 book ai didi

python - 根据多个值过滤字典列表

转载 作者:行者123 更新时间:2023-11-28 20:00:30 24 4
gpt4 key购买 nike

我有一个字典列表,我想根据多个条件对其进行过滤。列表的简化版本如下所示:

orders = [{"name": "v", "price": 123, "location": "Mars"}, 
{"name": "x", "price": 223, "location": "Mars"},
{"name": "x", "price": 124, "location": "Mars"},
{"name": "y", "price": 456, "location": "Mars"},
{"name": "z", "price": 123, "location": "Mars"},
{"name": "z", "price": 5623, "location": "Mars"}]

我希望最终得到一个列表,其中包含具有相同“名称”键的每本词典价格最低的词典。例如,上面会变成:

minimums = [{"name": "v", "price": 123, "location": "Mars"},
{"name": "x", "price": 124, "location": "Mars"},
{"name": "y", "price": 456, "location": "Mars"},
{"name": "z", "price": 123, "location": "Mars"}]

我已经通过讨厌的嵌套 if 语句和 for 循环实现了这一点,但是我希望有一种更“Pythonic”的方式来实现这一目标。

重复使用同一个列表或创建一个新列表都可以。

谢谢你的帮助。

编辑:感谢您的回答,我尝试使用以下代码对每个问题进行计时

print("Number of dictionaries in orders: " + str(len(orders)))

t0 = time.time()
sorted_orders = sorted(orders, key=lambda i: i["name"])
t1 = time.time()
sorting_time = (t1 - t0)

t0 = time.time()
listcomp_wikiben = [x for x in orders if all(x["price"] <= y["price"] for y in orders if x["name"] == y["name"])]
t1 = time.time()
print("listcomp_wikiben: " + str(t1 - t0))

t0 = time.time()
itertools_MrGeek = [min(g[1], key=lambda x: x['price']) for g in groupby(sorted_orders, lambda o: o['name'])]
t1 = time.time()
print("itertools_MrGeek: " + str(t1 - t0 + sorting_time))

t0 = time.time()
itertools_Cory = [min(g, key=lambda j: j["price"]) for k,g in groupby(sorted_orders, key=lambda i: i["name"])]
t1 = time.time()
print("itertools_CoryKramer: " + str(t1 - t0 + sorting_time))

t0 = time.time()
pandas_Trenton = pd.DataFrame(orders)
pandas_Trenton.groupby(['name'])['price'].min()
t1 = time.time()
print("pandas_Trenton_M: " + str(t1 - t0))

结果是:

Number of dictionaries in orders: 20867
listcomp_wikiben: 39.78123s
itertools_MrGeek: 0.01562s
itertools_CoryKramer: 0.01565s
pandas_Trenton_M: 0.29685s

最佳答案

如果您首先按 "name" 对列表进行排序,则可以使用 itertools.groupby 对它们进行分组,然后使用 min 和lambda 在每个组中找到最小的 "price"

>>> from itertools import groupby
>>> sorted_orders = sorted(orders, key=lambda i: i["name"])
>>> [min(g, key=lambda j: j["price"]) for k,g in groupby(sorted_orders , key=lambda i: i["name"])]
[{'name': 'v', 'price': 123, 'location': 'Mars'},
{'name': 'x', 'price': 124, 'location': 'Mars'},
{'name': 'y', 'price': 456, 'location': 'Mars'},
{'name': 'z', 'price': 123, 'location': 'Mars'}]

关于python - 根据多个值过滤字典列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57597433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com