命名元组

命名元组(实例)
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))

原文地址:https://www.cnblogs.com/liguangxu/p/5368677.html