命名元组

命名元组(实例)
import collections

 

Sale=collections.namedtuple("Sale","productid customerid date quantity price")

#productied customerid date quantity price对应元组中的(432,921,“2008-09-14”,3,7.99)
sales=[]
sales.append(Sale(432,921,"2008-09-14",3,7.99))
sales.append(Sale(419,874,"2008-09-15",1,18.49))
total=0

for sale in sales:

  total+=sale.quantity*sale.price
  print ("total ${0:.2f}".format(total))

posted @ 2016-04-08 16:28  ppppppy  阅读(149)  评论(0编辑  收藏  举报