python中使用heapq查看最大与最小的N个元素列表

时间:2023-01-01 22:43:22

怎么从一个集合中获取最大或最小的N个元素列表?

heapq模块有两个函数:nlargest() 和 nsmallest() 可以完美解决这个问题。

In [39]: import heapq

In [40]: nums = [2, 5, 3, 4]

In [41]: heapq.nlargest(2,nums)
Out[41]: [5, 4] In [42]: heapq.nsmallest(2,nums)
Out[42]: [2, 3]

两个函数都能接受一个关键字参数,用于更复杂的数据结构中:

以price值进行比较

portfolio = [
{'name': 'IBM', 'shares': 100, 'price': 91.1},
{'name': 'AAPL', 'shares': 50, 'price': 543.22},
{'name': 'FB', 'shares': 200, 'price': 21.09},
{'name': 'HPQ', 'shares': 35, 'price': 31.75},
{'name': 'YHOO', 'shares': 45, 'price': 16.35},
{'name': 'ACME', 'shares': 75, 'price': 115.65}
]
cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price'])
expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])

In [33]: heapq.nlargest(3, portfolio, key=lambda s: s['price'])
Out[33]:
[{'name': 'AAPL', 'price': 543.22, 'shares': 50},
{'name': 'ACME', 'price': 115.65, 'shares': 75},
{'name': 'IBM', 'price': 91.1, 'shares': 100}]

等于

In [34]: sorted(portfolio, key=lambda s: s['price'],reverse=True)[:3]
Out[34]:
[{'name': 'AAPL', 'price': 543.22, 'shares': 50},
{'name': 'ACME', 'price': 115.65, 'shares': 75},
{'name': 'IBM', 'price': 91.1, 'shares': 100}]

对字典排序

In [21]: a = {'a': 1, 'b': 2, 'c': 4, 'd': 3}
按字典值排序
In [22]: sorted(a.items(), key=lambda s: s[1])
Out[22]: [('a', 1), ('b', 2), ('d', 3), ('c', 4)]
按字典键排序
In [23]: sorted(a.items(), key=lambda s: s[0])
Out[23]: [('a', 1), ('b', 2), ('c', 4), ('d', 3)]