8

假设我有一本总水果词典:

Fruits = {"apple":8, "banana":3, "lemon":5, "pineapple":2,}

我希望输出是

["pineapple"]

因为菠萝的价值最低。或者如果我有这个:

Colour = {"blue":5, "green":2, "purple":6, "red":2}

输出将是:

["green","red"]

因为绿色和红色的价值都最低。

那么如何返回字典中的最小值呢?

4

8 回答 8

16

可以做两遍:

>>> colour
{'blue': 5, 'purple': 6, 'green': 2, 'red': 2}
>>> min_val = min(colour.itervalues())
>>> [k for k, v in colour.iteritems() if v == min_val]
['green', 'red']
  1. 找到字典值的最小值
  2. 然后返回并提取该值所在的密钥...

另一种选择(需要一些导入,这意味着如果需要,您可以使用 n 个)-此代码只使用第一个(这将是最小值):

from itertools import groupby
from operator import itemgetter

ordered = sorted(colour.iteritems(), key=itemgetter(1))
bykey = groupby(ordered, key=itemgetter(1))
print map(itemgetter(0), next(bykey)[1])
# ['green', 'red']
于 2013-04-19T07:17:06.957 回答
4

我想说最好的选择是通过两次:

min_value = min(dict.values())
result = [key for key, value in dict.iteritems() if value == min_value]

您可以通过显式循环进行单次传递:

result = []
min_value = None
for key, value in dict.iteritems():
    if min_value is None or value < min_value:
        min_value = value
        result = []
    if value == min_value:
        result.append(key)

但这会更慢(可能在 PyPy 中除外)

于 2013-04-19T07:22:24.723 回答
1

只是一个选择:

from collections import defaultdict
from operator import itemgetter

Fruits = {"apple":8, "banana":3, "lemon":5, "pineapple":2,}
Colour = {"blue":5, "green":2, "purple":6, "red":2}


def get_res(dVals):
    res = defaultdict(list)
    for k, v in dVals.items():
        res[v].append(k)
    return min(res.items(), key=itemgetter(0))[1]

print get_res(Fruits)
print get_res(Colour)
于 2013-04-19T07:11:27.890 回答
0
colors = {"blue":5, "green":2, "purple":6, "red":2}# {"apple":8, "banana":3, "lemon":5, "pineapple":2,}

sorted_items = sorted(colors.items(), key=lambda t: t[1])
print (sorted_items)


min_val = sorted_items[0][1]

for t in sorted_items:
    if t[1] == min_val:
        print(t[0])
    else:
        break




--output:--
[('green', 2), ('red', 2), ('blue', 5), ('purple', 6)]

green
red
于 2013-04-19T07:24:53.133 回答
0
import itertools
def getmin(dictionary):
    gen = ((j,i) for i,j in dictionary.iteritems())
    items = sorted(gen)
    minimum = items.pop(0)
    rest = itertools.takewhile(lambda item:item[0]==minimum[0],items)
    return [x[1] for x in itertools.chain([minimum],rest)]

此方法使用schwartzian 变换来利用本机排序(无需密钥)。它根据字典中键的值对项进行排序,取最小值,并取所有相同的项。

于 2013-04-19T07:28:29.477 回答
0

如我所见,它适用于这样的脏 lambda 函数:

min(Fruits,key=lambda x:Fruits[x])

我猜它只会返回一个值,但仍然相当不错。:)

于 2016-07-15T16:39:13.793 回答
0
ans =[min(Fruits, key=Fruits.get)]
print(ans)

上面的代码在字典中查找与最小值对应的键。它找到 dictionary(Fruits) 的键,其中函数 Fruits.get(key) 等效于 Fruits[key] 返回最低可比较值。

对于具有最小值的多个键,首先从字典 Fruits 中获取最小值,然后使用 for 循环从字典中获取对应的键,如下所示。

min_value= min(Fruits.values())
ans= [key for key in Fruits if Fruits[key]==min_value]
print(ans)
于 2020-09-03T13:52:31.163 回答
-1
minimum = []      # creates empty list for all possible minimum keys
def min(dict):    # defines a function called minimum taking a argument dict
    m= min(dict.values())     # stating minimum value in dict
    for key, values in dict.items():     # iterating through key and values in items of dictionary
        if value == m:        # checking if value is minimum
            minimum.append(key)      # appending minimum values to the empty list created
    return minimum         # returning the list
于 2017-07-07T18:26:34.527 回答