I am new to python, I am interested in to count repeated keys of dictionary in this case dates. I have this peace of code since in operator checks membership of keys its only checking once of the repeated keys.
import datetime
from collections import defaultdict
amount = 0
d = {'12-Aug-2018' : 22 ,
'11-Aug-2018' : 5,
'10-Aug-2018' : 3,
'10-Aug-2018' : 2,
'9-Aug-2018' : 1,
'9-Aug-2018' : 6
}
dcount = defaultdict(lambda:0)
for date in d:
dcount[date] +=1
print (d[date])
print(dcount.items())
Output:
22
5
2
6
dict_items([('12-Aug-2018', 1), ('11-Aug-2018', 1), ('10-Aug-2018', 1), ('9-Aug-2018', 1)])
value 3 of the key 10-Aug-2018 and value 1 of 9-Aug-2018' are skipped.
Desired output:
([('12-Aug-2018', 1), ('11-Aug-2018', 1), ('10-Aug-2018', 2), ('9-Aug-2018', 2)])
Any help? Thanks