Skip to content

Instantly share code, notes, and snippets.

@H4medRostami
Created June 6, 2019 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save H4medRostami/c585d414fbf875f10ee36b5922f87bb8 to your computer and use it in GitHub Desktop.
Save H4medRostami/c585d414fbf875f10ee36b5922f87bb8 to your computer and use it in GitHub Desktop.
import heapq
nums=[1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
print(heapq.nlargest(3, nums))
print(heapq.nsmallest(3, nums))
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=lambdas:s['price'])
expensive=heapq.nlargest(3,portfolio,key=lambdas:s['price'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment