Skip to content

Instantly share code, notes, and snippets.

@Averroes
Created April 10, 2015 14:10
Show Gist options
  • Select an option

  • Save Averroes/023de0ce3e90cbb5bb31 to your computer and use it in GitHub Desktop.

Select an option

Save Averroes/023de0ce3e90cbb5bb31 to your computer and use it in GitHub Desktop.
mapping names to sequence elements
# 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