Python 为什么在我返回项目时不显示它?我得到的是记忆的位置
基本上,我要做的是显示支出列表中属于同一类别的所有项目,然后显示每个类别的总额,最后显示总总额。例如:Python 为什么在我返回项目时不显示它?我得到的是记忆的位置,python,python-3.x,class,Python,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
$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
根据之前的评论进行了一些更改您的问题是这两行(加上其他几个点): 导致错误的最小示例:
exp = {}
exp['test'] = []
# exp = {'test': []}
exp['test']['Amount'] = [] # Amount throws an error because exp['test'] is a list.
我想你的意思是:
exp[types] = {}
exp[types]['Amount'] = []
但你的另一个逻辑令人困惑:
# Here you assume exp[item._type] is of type list because you use append on it
exp[item._type].append(item)
# But here you assume exp[item._type] is of type dict because you call ['Amount'] on it...
exp[item._type]['Amount'].append(item._amount)
请粘贴错误回溯,它准确地告诉您是哪一行导致了问题,以及导致问题的那几行。
\uuuu str\uuuu()
不应该打印任何内容,它的工作是返回对象的字符串版本(调用者可以选择打印,也可以选择完全执行其他操作)。
exp[types] = {}
exp[types]['Amount'] = []
# Here you assume exp[item._type] is of type list because you use append on it
exp[item._type].append(item)
# But here you assume exp[item._type] is of type dict because you call ['Amount'] on it...
exp[item._type]['Amount'].append(item._amount)