Created
June 6, 2019 19:25
-
-
Save H4medRostami/c585d414fbf875f10ee36b5922f87bb8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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