【发布时间】:2015-03-04 04:56:28
【问题描述】:
鉴于[(Currency1, amount1),(Currency2, amount2)] 的 turple 格式列表,我想按关键货币汇总每个金额,但它不起作用。我试过了:
>>> mylist=[(‘USD’,1000),(‘THB’,25),(‘USD’,3500)]
>>> for i in mylist:
... sum += i[1]
...
Traceback (most recent call last):
File “<stdin>“, line 2, in <module>
TypeError: unsupported operand type(s) for +=: ‘builtin_function_or_method’ and ‘int’
>>>
我想知道如何按货币计算总金额,这将作为 turple 列表返回,如下所示:
[(‘USD’, 4500), (‘THB’, 25)]
请帮忙谢谢。
【问题讨论】:
-
你的预期输出是什么?