Last active
June 27, 2017 17:46
-
-
Save mikeckennedy/52d7c94d96dbef5d9e776f2d7b8b9fb7 to your computer and use it in GitHub Desktop.
Sorting example
This file contains hidden or 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 collections | |
Stock = collections.namedtuple('Stock', 'symbol, open, high, low, close, volume') | |
stocks = [ | |
Stock('AAL', 49, 49.1, 48.47, 48.63, 11901883), | |
Stock('AAPL', 145.13, 147.16, 145.11, 146.28, 33917635), | |
Stock('ADBE', 143.75, 145.59, 143.06, 145.41, 3383524), | |
Stock('AMZN', 1002.54, 1004.62, 998.02, 1003.74, 2832807), | |
Stock('GOOGL', 975.5, 986.62, 974.46, 986.09, 1524905), | |
Stock('MAT', 20.22, 20.75, 20.11, 20.68, 10603967), | |
Stock('MSFT', 70.09, 71.25, 69.92, 71.21, 26803888), | |
Stock('NTES', 320, 333.78, 319, 333.56, 1356386), | |
] | |
print(stocks) | |
# Remember lambda expressions look like this | |
# lambda value: value.property | |
print("Stock with highest high: ") | |
ans = sorted(stocks)[0] | |
print(ans.high, ans.symbol) | |
print("Stock with lowest low: ") | |
ans = sorted(stocks)[0] | |
print(ans.symbol, ans.low) | |
print("highest volume: ") | |
ans = sorted(stocks)[0] | |
print(ans.symbol, "{:,}".format(ans.volume)) | |
print("Stock with lowest volume: ") | |
ans = sorted(stocks)[0] | |
print(ans.symbol, "{:,}".format(ans.volume)) | |
stocks.sort() | |
print(stocks) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment