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 SO article), 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.
我想找到最小()和最大值()的价格。现在,我可以使用带有lambda表达式的键(如另一篇SO文章中所示)轻松地对其进行排序,因此如果没有其他方法,我就不会陷入困境。然而,根据我所看到的,在Python中几乎总是有一种直接的方式,所以这是一个让我了解更多的机会。
4 个解决方案
#1
33
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 int
s):
如果您只想遍历列表一次,您可以尝试这个(假设值可以表示为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)
#2
113
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.
它不仅告诉你最大价格是多少,还告诉你哪个项目最昂贵。
#3
25
I think the most direct (and most Pythonic) expression would be something like:
我认为最直接(也是最符合python)的表达方式应该是:
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!
这就避免了排序列表的开销——而且,通过使用生成器表达式而不是列表理解——实际上也避免了创建任何列表。高效、直接、可读的…神谕的!
#4
6
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)
#1
33
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 int
s):
如果您只想遍历列表一次,您可以尝试这个(假设值可以表示为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)
#2
113
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.
它不仅告诉你最大价格是多少,还告诉你哪个项目最昂贵。
#3
25
I think the most direct (and most Pythonic) expression would be something like:
我认为最直接(也是最符合python)的表达方式应该是:
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!
这就避免了排序列表的开销——而且,通过使用生成器表达式而不是列表理解——实际上也避免了创建任何列表。高效、直接、可读的…神谕的!
#4
6
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)