返回字典中的三个最大值

时间:2018-11-20 10:03:55

标签: python sorting dictionary

我有以下字典:

'{0: 0, 1: 11, 2: 26, 3: 43, 4: 14, 5: 29, 6: 34, 7: 49, 8: 49, 9: 108, 10: 124, 11: 108, 12: 361, 13: 290, 14: 2118, 15: 5408, 16: 43473, 17: 109462, 18: 111490, 19: 244675, 20: 115878, 21: 6960}'

为此字典,我想编写一个函数,该函数返回具有最高值的三个键值对(因此,在本例中为键18、19、20)。

我提出了以下建议:

cachedict = nr_of_objects_per_century() #Dictionary mentioned above

def top_3_centuries():
        max_nr_works_list = sorted(cachedict.values())
        top_3_values = []
        for i in range(len(max_nr_works_list)-3, len(max_nr_works_list)):
            top_3_values.append(max_nr_works_list[i])
            print(top_3_values)

这给了我想要查找的最大值列表。但是我如何从这里开始?有没有一种方法可以不进行反向查找(对于字典来说这比较慢,对吗?),我觉得我可以更高效/ Python地完成此任务。

8 个答案:

答案 0 :(得分:6)

您还可以将collections.Countermost_common(内部使用堆队列)一起使用:

from collections import Counter

dct = {0: 0, 1: 11, 2: 26, 3: 43, 4: 14, 5: 29, 6: 34, 7: 49, 8: 49, 
       9: 108, 10: 124, 11: 108, 12: 361, 13: 290, 14: 2118, 15: 5408, 
       16: 43473, 17: 109462, 18: 111490, 19: 244675, 20: 115878, 21: 6960}

count = Counter(dct)
print(count.most_common(3))  # [(19, 244675), (20, 115878), (18, 111490)]

答案 1 :(得分:6)

heapq.nlargest

您可以通过使用堆队列来避免在此处进行完整排序:

from heapq import nlargest
from operator import itemgetter

dct = {0: 0, 1: 11, 2: 26, 3: 43, 4: 14, 5: 29, 6: 34, 7: 49, 8: 49,
       9: 108, 10: 124, 11: 108, 12: 361, 13: 290, 14: 2118, 15: 5408,
       16: 43473, 17: 109462, 18: 111490, 19: 244675, 20: 115878, 21: 6960}

res = nlargest(3, dct.items(), key=itemgetter(1))

print(res)
# [(19, 244675), (20, 115878), (18, 111490)]

答案 2 :(得分:3)

您可以使用此:

a = {0: 0, 1: 11, 2: 26, 3: 43, 4: 14, 5: 29, 6: 34, 7: 49, 8: 49,
       9: 108, 10: 124, 11: 108, 12: 361, 13: 290, 14: 2118, 15: 5408,
       16: 43473, 17: 109462, 18: 111490, 19: 244675, 20: 115878, 21: 6960}

l = sorted(list(a.items()), key=lambda tup: tup[1], reverse=True)[:3]
print(l) # [(19, 244675), (20, 115878), (18, 111490)]

它将字典a转换为元组列表,按tup[1]排序,将其反转并获得前3个匹配。

答案 3 :(得分:2)

您可以这样做:

dct = {0: 0, 1: 11, 2: 26, 3: 43, 4: 14, 5: 29, 6: 34, 7: 49, 8: 49, 9: 108, 10: 124, 11: 108, 12: 361, 13: 290, 14: 2118, 15: 5408, 16: 43473, 17: 109462, 18: 111490, 19: 244675, 20: 115878, 21: 6960}

res = [next(k for k in dct if dct[k]==v) for v in sorted(dct.values(), reverse=True)[:3]]
print(res)  # -> [19, 20, 18]

崩溃:

  • sorted(dct.values(), reverse=True)[:3] ::接受3个最大词典值。
  • next(k for k in dct if dct[k]==v) ::返回字典键,其值是上述3个值之一(迭代)。

答案 4 :(得分:2)

只需两个简单的步骤:

aux = sorted([(v,k) for (k,v) in dic.items()])
res = [(v,k) for (k,v) in aux[-3:]] 
#[(18, 111490), (20, 115878), (19, 244675)]

在此示例中比nlargestCounter.most_common快。

答案 5 :(得分:2)

这将返回您想要的内容:

d = {0: 0, 1: 11, 2: 26, 3: 43, 4: 14, 5: 29, 6: 34, 7: 49, 8: 49, 9: 108, 10: 124, 11: 108, 12: 361, 13: 290, 14: 2118, 15: 5408, 16: 43473, 17: 109462, 18: 111490, 19: 244675, 20: 115878, 21: 6960}

print(sorted([(i,j) for i, j in d.items() if j in (sorted(d.values())[-3:])])[-3:])
#[(18, 111490), (19, 244675), (20, 115878)]

答案 6 :(得分:1)

d = {0: 0, 1: 11, 2: 26, 3: 43, 4: 14, 5: 29, 6: 34, 7: 49, 8: 49, 9: 108, 10: 124, 11: 108, 12: 361, 13: 290, 14: 2118, 15: 5408, 16: 43473, 17: 109462, 18: 111490, 19: 244675, 20: 115878, 21: 6960}

d_items_sorted = sorted(d.items(), key=lambda x: x[1], reverse=True)

d_items_sorted[:3]

返回:

[(19, 244675), (20, 115878), (18, 111490)]

这是我能获得的最简单的代码,但是对字典的排序为O(nlogn),那么您应该可以在O(n)中进行同样的操作

答案 7 :(得分:0)

您是在寻找简单/算法简单的最有效方法还是最佳方法?

如果是后者,则应考虑将字典项按元组排序(可以使用cachedict.items()获得它们),如此答案https://stackoverflow.com/a/613218/10453363

只需按值对元组进行排序,然后获取最后3个元组(即键/值对)

相关问题