mylist=[('USD',1000),('THB',25),('USD',3500)]
# Initialise the aggregator dictionary
res = {}
# Populate the aggregator dictionary
for cur, val in mylist:
if cur in res:
# If the currency already exists, add the value to its total
res[cur] += val
else:
# else create a new key/value pair in the dictionary.
res[cur] = val
# And some nice output
for key in res:
print('{:>5}: {:>6}'.format(key, res[key]))
如果您想将同一种货币的价值相加,这有助于:
相关问题 更多 >
编程相关推荐