Python的dicts列表,获取最大值索引

时间:2022-02-18 18:06:00

I'm trying to get the index of the dictionary with the max 'size' in a list of dictionaries like the following:

我正在尝试在字典列表中获取具有最大'大小'的字典索引,如下所示:

ld = [{'prop': 'foo', 'size': 100}, {'prop': 'boo', 'size': 200}]

with the following code I can take the maximum size:

使用以下代码我可以采用最大尺寸:

items = [x['size'] for x in ld]print max(items)

How can I take its index now? Is there an easy way?

我现在该如何获取其索引?有一个简单的方法吗?

Test:

I just figured i can do that:

我只想我能做到这一点:

items = [x['size'] for x in ld]max_val = max(items)print items.index(max_val)

is it correct?

这是对的吗?

2 个解决方案

#1


16  

Tell max() how to calculate the maximum for a sequence of indices:

告诉max()如何计算索引序列的最大值:

max(xrange(len(ld)), key=lambda index: ld[index]['size'])

This'll return the index for which the size key is the highest:

这将返回大小键最高的索引:

>>> ld = [{'prop': 'foo', 'size': 100}, {'prop': 'boo', 'size': 200}]>>> max(xrange(len(ld)), key=lambda index: ld[index]['size'])1>>> ld[1]{'size': 200, 'prop': 'boo'}

If you wanted that dictionary all along, then you could just use:

如果您一直想要这本字典,那么您可以使用:

max(ld, key=lambda d: d['size'])

and to get both the index and the dictionary, you could use enumerate() here:

并获得索引和字典,你可以在这里使用enumerate():

max(enumerate(ld), key=lambda item: item[1]['size'])

Some more demoing:

一些演示:

>>> max(ld, key=lambda d: d['size']){'size': 200, 'prop': 'boo'}>>> max(enumerate(ld), key=lambda item: item[1]['size'])(1, {'size': 200, 'prop': 'boo'})

The key function is passed each element in the input sequence in turn, and max() will pick the element where the return value of that key function is highest.

键函数依次传递输入序列中的每个元素,max()将选择该键函数的返回值最高的元素。

Using a separate list to extract all the size values then mapping that back to your original list is not very efficient (you now need to iterate over the list twice). list.index() cannot work as it has to match the whole dictionary, not just one value in it.

使用单独的列表提取所有大小值然后将其映射回原始列表不是非常有效(您现在需要迭代列表两次)。 list.index()无法工作,因为它必须匹配整个字典,而不仅仅是其中的一个值。

#2


3  

You can pass the enumerate(ld) to max function with a proper key :

您可以使用正确的密钥将枚举(ld)传递给max函数:

>>> max(enumerate(ld),key=lambda (x,y):y['size'])[0]1

#1


16  

Tell max() how to calculate the maximum for a sequence of indices:

告诉max()如何计算索引序列的最大值:

max(xrange(len(ld)), key=lambda index: ld[index]['size'])

This'll return the index for which the size key is the highest:

这将返回大小键最高的索引:

>>> ld = [{'prop': 'foo', 'size': 100}, {'prop': 'boo', 'size': 200}]>>> max(xrange(len(ld)), key=lambda index: ld[index]['size'])1>>> ld[1]{'size': 200, 'prop': 'boo'}

If you wanted that dictionary all along, then you could just use:

如果您一直想要这本字典,那么您可以使用:

max(ld, key=lambda d: d['size'])

and to get both the index and the dictionary, you could use enumerate() here:

并获得索引和字典,你可以在这里使用enumerate():

max(enumerate(ld), key=lambda item: item[1]['size'])

Some more demoing:

一些演示:

>>> max(ld, key=lambda d: d['size']){'size': 200, 'prop': 'boo'}>>> max(enumerate(ld), key=lambda item: item[1]['size'])(1, {'size': 200, 'prop': 'boo'})

The key function is passed each element in the input sequence in turn, and max() will pick the element where the return value of that key function is highest.

键函数依次传递输入序列中的每个元素,max()将选择该键函数的返回值最高的元素。

Using a separate list to extract all the size values then mapping that back to your original list is not very efficient (you now need to iterate over the list twice). list.index() cannot work as it has to match the whole dictionary, not just one value in it.

使用单独的列表提取所有大小值然后将其映射回原始列表不是非常有效(您现在需要迭代列表两次)。 list.index()无法工作,因为它必须匹配整个字典,而不仅仅是其中的一个值。

#2


3  

You can pass the enumerate(ld) to max function with a proper key :

您可以使用正确的密钥将枚举(ld)传递给max函数:

>>> max(enumerate(ld),key=lambda (x,y):y['size'])[0]1