考虑一个带有一个属性和一个属性的dict
s列表:date
amount
transactions = [
{'date': '2013-05-12', 'amount': 1723},
{'date': '2013-07-23', 'amount': 4523},
{'date': '2013-02-01', 'amount': 2984},
]
我想添加一个 balance 属性,但为此我必须按date
顺序遍历列表:
balance = 0
for t in transactsions: # Order by date
balance += t['amount']
t['balance'] = balance
怎么办?如果我用具有和属性的对象替换dicts
,那Transaction
有可能吗?date
amount