Created
June 6, 2019 19:47
-
-
Save H4medRostami/c11fa0e1137943b0efb81e1a67b70272 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
prices={'ACME':45.23, | |
'AAPL':612.78, | |
'IBM':205.55, | |
'HPQ':37.20,'FB':10.75} | |
min_price=min(zip(prices.values(),prices.keys()))# min_price is (10.75, 'FB') | |
max_price=max(zip(prices.values(),prices.keys()))# max_price is (612.78, 'AAPL') | |
#Similarly, to rank the data, use zip() with sorted(), as in the following: | |
prices_sorted=sorted(zip(prices.values(),prices.keys())) | |
# prices_sorted is [(10.75, 'FB'), (37.2, 'HPQ'), | |
# (45.23, 'ACME'), (205.55, 'IBM'), | |
# (612.78, 'AAPL')] | |
#When doing these calculations, be aware that zip()creates an iterator that can only beconsumed once. For example, the following code is an error: | |
prices_and_names=zip(prices.values(),prices.keys()) | |
print(min(prices_and_names))# OK | |
print(max(prices_and_names))# ValueError: max() arg is an empty sequence |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment