命名元组

it2024-10-13  17

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

最新回复(0)