Python 3.x 使用字符串dunder显示下面的输出?
我试图显示支出列表中属于同一类别的所有项目,然后是每个类别的总额,最后是总总额。例如:Python 3.x 使用字符串dunder显示下面的输出?,python-3.x,class,Python 3.x,Class,我试图显示支出列表中属于同一类别的所有项目,然后是每个类别的总额,最后是总总额。例如: $12.00 Mon, 12 Aug 2019 Food $15.25 Mon, 12 Aug 2019 Food Total for Food = $27.25 $8.40 Wed, 14 Aug 2019 Drinks Total for Drinks = $8.40 $33.00 Tue, 13 Aug 2019 Entertainment Total for Entertainment = $33.0
$12.00 Mon, 12 Aug 2019 Food
$15.25 Mon, 12 Aug 2019 Food
Total for Food = $27.25
$8.40 Wed, 14 Aug 2019 Drinks
Total for Drinks = $8.40
$33.00 Tue, 13 Aug 2019 Entertainment
Total for Entertainment = $33.00
Overall Total = $178.55
这是到目前为止我的代码。我想做的是把分类中的所有项目都放到一本字典里,里面有所有的细节,另外一本字典里只有数量,这样我就可以总结了
class ExpenditureList:
_types = ['Food','Entertainment', 'Drinks']
def __init__(self):
self._expenditures = []
@classmethod
def ExpenditureTypes(cls):
return cls._types
def __str__(self):
exp = {}
amount = {}
temp = None
for types in ExpenditureList._types:
exp[types] = []
amount[types] = []
for item in self._expenditures:
if item._type == ExpenditureList._types[0]:
exp[item._type].append(item)
amount[item._type].append(item._amount)
elif item._type == ExpenditureList._types[1]:
exp[item._type].append(item)
amount[item._type].append(item._amount)
elif item._type == ExpenditureList._types[2]:
exp[item._type].append(item)
amount[item._type].append(item._amount)
这就是我所做的,我不确定如何返回以获得上述输出