如何在字典列表中查找公共键的最小/最大值?

发布于 2024-10-22 04:34:32 字数 340 浏览 2 评论 0原文

我有一个像这样的字典列表:

[{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}]

我想找到 min()max() 价格。现在,我可以使用带有 lambda 表达式的键(如另一篇 Stack Overflow 帖子中所示)轻松地对此进行排序,因此如果没有其他方法,我就不会陷入困境。然而,据我所知,Python 中几乎总是有直接的方法,所以这对我来说是一个学习更多知识的机会。

I have a list of dictionaries like so:

[{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}]

I want to find the min() and max() prices. Now, I can sort this easily enough using a key with a lambda expression (as found in another Stack Overflow post), so if there is no other way I'm not stuck. However, from what I've seen there is almost always a direct way in Python, so this is an opportunity for me to learn a bit more.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

看透却不说透 2024-10-29 04:34:32
lst = [{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}]

maxPricedItem = max(lst, key=lambda x:x['price'])
minPricedItem = min(lst, key=lambda x:x['price'])

这不仅告诉您最高价格是多少,还告诉您哪件商品最贵。

lst = [{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}]

maxPricedItem = max(lst, key=lambda x:x['price'])
minPricedItem = min(lst, key=lambda x:x['price'])

This tells you not just what the max price is but also which item is most expensive.

梦旅人picnic 2024-10-29 04:34:32

有几种选择。这是一个简单的方法:

seq = [x['the_key'] for x in dict_list]
min(seq)
max(seq)

[编辑]

如果您只想遍历列表一次,您可以尝试此操作(假设值可以表示为 int ):

import sys

lo,hi = sys.maxint,-sys.maxint-1
for x in (item['the_key'] for item in dict_list):
    lo,hi = min(x,lo),max(x,hi)

There are several options. Here is a straight-forward one:

seq = [x['the_key'] for x in dict_list]
min(seq)
max(seq)

[Edit]

If you only wanted to iterate through the list once, you could try this (assuming the values could be represented as ints):

import sys

lo,hi = sys.maxint,-sys.maxint-1
for x in (item['the_key'] for item in dict_list):
    lo,hi = min(x,lo),max(x,hi)
爱,才寂寞 2024-10-29 04:34:32

我认为最直接(也是最Pythonic)的表达式类似于:

min_price = min(item['price'] for item in items)

这避免了对列表进行排序的开销——并且通过使用生成器表达式而不是列表理解——实际上也避免了创建任何列表。高效、直接、可读……Pythonic!

I think the most direct (and most Pythonic) expression would be something like:

min_price = min(item['price'] for item in items)

This avoids the overhead of sorting the list -- and, by using a generator expression, instead of a list comprehension -- actually avoids creating any lists, as well. Efficient, direct, readable... Pythonic!

情域 2024-10-29 04:34:32

一种答案是将您的字典映射到生成器表达式中感兴趣的值,然后应用内置的 minmax

myMax = max(d['price'] for d in myList)
myMin = min(d['price'] for d in myList)

One answer would be mapping your dicts to the value of interest inside a generator expression, and then applying the built-ins min and max.

myMax = max(d['price'] for d in myList)
myMin = min(d['price'] for d in myList)
江湖彼岸 2024-10-29 04:34:32

也可以使用这个:

from operator import itemgetter

lst = [{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}]  
max(map(itemgetter('price'), lst))

can also use this:

from operator import itemgetter

lst = [{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}]  
max(map(itemgetter('price'), lst))
噩梦成真你也成魔 2024-10-29 04:34:32

并添加到这个伟大的页面:通用便捷功能中的最佳答案:


def takeMaxFromDictList(listOfDicts: list, keyToLookAt: str) -> dict:
  return max( listOfDicts, key=lambda x: x[keyToLookAt] )

# -------------------------------------------------------------------

examplelist = [{'score': 0.995, 'label': 'buildings'},
               {'score': 0.002, 'label': 'mountain'},
               {'score': 0.001, 'label': 'forest'}]
 
print ( takeMaxFromDictList(examplelist, 'score') )

>>> {'score': 0.995, 'label': 'buildings'}

And to add to this great page: the top answer in a generic convenient function:


def takeMaxFromDictList(listOfDicts: list, keyToLookAt: str) -> dict:
  return max( listOfDicts, key=lambda x: x[keyToLookAt] )

# -------------------------------------------------------------------

examplelist = [{'score': 0.995, 'label': 'buildings'},
               {'score': 0.002, 'label': 'mountain'},
               {'score': 0.001, 'label': 'forest'}]
 
print ( takeMaxFromDictList(examplelist, 'score') )

>>> {'score': 0.995, 'label': 'buildings'}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文