Created
April 10, 2015 14:10
-
-
Save Averroes/023de0ce3e90cbb5bb31 to your computer and use it in GitHub Desktop.
mapping names to sequence elements
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
| # example.py | |
| from collections import namedtuple | |
| Stock = namedtuple('Stock', ['name', 'shares', 'price']) | |
| def compute_cost(records): | |
| total = 0.0 | |
| for rec in records: | |
| s = Stock(*rec) | |
| total += s.shares * s.price | |
| return total | |
| # Some Data | |
| records = [ | |
| ('GOOG', 100, 490.1), | |
| ('ACME', 100, 123.45), | |
| ('IBM', 50, 91.15) | |
| ] | |
| print(compute_cost(records)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment