d = dict()
for letter, number in line:
d[letter] = number + d.get(letter, 0)
for x in sorted(d.keys()):
print(x, d[x])
The second param in d.get
gives a default value of 0
if letter
is not already in the dictionary.
The sorted()
func makes sure you are going in incr order wrt to keys.
We have a cumulative mapping and then print it out, short and sweet :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…