在词典字典中查找最小元素

时间:2021-07-23 21:36:56

I need to find what element of apple has the minimum size.

我需要找到苹果的最小尺寸。

Tnx for all answers. But there is one problem: I use Python 2.4.2 (I can't change it) and function min haven't key arg. Yes, I need key of apple

Tnx所有答案。但是有一个问题:我使用Python 2.4.2(我不能改变它)和函数min没有关键arg。是的,我需要苹果的钥匙

apple = {1:{'size':12,'color':'red'},2:{'size':10,'color':'green'}}

6 个解决方案

#1


10  

Python has a very nice parameter for the min function that allows using an arbitrary function to be minified instead of just using comparison on the elements:

Python为min函数提供了一个非常好的参数,它允许使用任意函数来缩小,而不是仅仅使用元素上的比较:

result = min(apple.values(), key=lambda x:x['size'])

The key parameter replaced in most cases the older idiom of decorate-process-undecorate that could have been applied here:

在大多数情况下,关键参数替换了可以在此处应用的decorate-process-undecorate的旧习语:

result = min((x['size'], x) for x in apple.values())[1]

If instead you want to know the number (key) of the apple (it's not clear in the question) then:

如果你想知道苹果的数量(关键)(问题中不清楚)那么:

result = min(apple.keys(), key=lambda x:apples[x]['size'])

or (old style)

或(旧式)

result = min((apples[x]['size'], x) for x in apple.keys())[1]

#2


11  

import operator
min(apple.values(), key=operator.itemgetter('size'))

will return you

会回报你

{'color': 'green', 'size': 10}

UPDATE: to get the index:

更新:获取索引:

min(apple, key=lambda k: apple[k]['size'])

#3


3  

Use min with a custom key function that returns the size of each item.

使用min与自定义键功能,返回每个项目的大小。

apple = {1:{'size':12,'color':'red'},2:{'size':10,'color':'green'}}
print min(apple.keys(), key=lambda k, a=apple: a[k]['size'])

Which prints:

哪个印刷品:

2

P.S. Since apple is a collection I would make it plural -- apples.

附:由于苹果是一个集合,我会把它复数 - 苹果。

#4


1  

Don't know if it's the fastest way to do it, but anyway:

不知道这是否是最快的方式,但无论如何:

>>> apple = [ {'size':12, 'color': 'red' }, { 'size':10, 'color':'green'} ]
>>> a = dict(map(lambda apple: (apple['size'], apple), apple))
>>> a
{10: {'color': 'green', 'size': 10}, 12: {'color': 'red', 'size': 12}}
>>> min = a[min(a.keys())]
>>> min
{'color': 'green', 'size': 10}

#5


1  

def get_min(apple):
    L = apple.values()
    m = L[0]
    for item in L:
        if item['size'] < m['size']:
            m = item
    return m

P.S. Not very pythonic but linear time

附:不是非常pythonic但线性时间

#6


0  

min(map(lambda a:[apple[a]['size'],a], apple))[1]

#1


10  

Python has a very nice parameter for the min function that allows using an arbitrary function to be minified instead of just using comparison on the elements:

Python为min函数提供了一个非常好的参数,它允许使用任意函数来缩小,而不是仅仅使用元素上的比较:

result = min(apple.values(), key=lambda x:x['size'])

The key parameter replaced in most cases the older idiom of decorate-process-undecorate that could have been applied here:

在大多数情况下,关键参数替换了可以在此处应用的decorate-process-undecorate的旧习语:

result = min((x['size'], x) for x in apple.values())[1]

If instead you want to know the number (key) of the apple (it's not clear in the question) then:

如果你想知道苹果的数量(关键)(问题中不清楚)那么:

result = min(apple.keys(), key=lambda x:apples[x]['size'])

or (old style)

或(旧式)

result = min((apples[x]['size'], x) for x in apple.keys())[1]

#2


11  

import operator
min(apple.values(), key=operator.itemgetter('size'))

will return you

会回报你

{'color': 'green', 'size': 10}

UPDATE: to get the index:

更新:获取索引:

min(apple, key=lambda k: apple[k]['size'])

#3


3  

Use min with a custom key function that returns the size of each item.

使用min与自定义键功能,返回每个项目的大小。

apple = {1:{'size':12,'color':'red'},2:{'size':10,'color':'green'}}
print min(apple.keys(), key=lambda k, a=apple: a[k]['size'])

Which prints:

哪个印刷品:

2

P.S. Since apple is a collection I would make it plural -- apples.

附:由于苹果是一个集合,我会把它复数 - 苹果。

#4


1  

Don't know if it's the fastest way to do it, but anyway:

不知道这是否是最快的方式,但无论如何:

>>> apple = [ {'size':12, 'color': 'red' }, { 'size':10, 'color':'green'} ]
>>> a = dict(map(lambda apple: (apple['size'], apple), apple))
>>> a
{10: {'color': 'green', 'size': 10}, 12: {'color': 'red', 'size': 12}}
>>> min = a[min(a.keys())]
>>> min
{'color': 'green', 'size': 10}

#5


1  

def get_min(apple):
    L = apple.values()
    m = L[0]
    for item in L:
        if item['size'] < m['size']:
            m = item
    return m

P.S. Not very pythonic but linear time

附:不是非常pythonic但线性时间

#6


0  

min(map(lambda a:[apple[a]['size'],a], apple))[1]