I'm attempting to create a data structure that keeps track of occurrences per month over a number of years. I have determined a dictionary of lists to be the best option. I want to create something like this structure (year: list of twelve integers representing occurrences per month):
yeardict = {
'2007':[0,1,2,0,3,4,1,3,4,0,6,3]
'2008':[0,1,2,0,3,4,1,3,5,0,6,3]
'2010':[7,1,3,0,2,6,0,6,1,8,1,4]
}
I am taking as an input, a dictionary that looks like this:
monthdict = {
'2007-03':4,
'2007-05':2,
'2008-02':8
etc.
}
I have my code loop through the second dictionary, first paying attention to the first 4 characters in the key (the year) and if that is NOT in the dictionary, then I initialize that key along with the value of twelve blank months in a list form: [0,0,0,0,0,0,0,0,0,0,0,0], then change the value of the item in that list of the position of the month to whatever the value is. If the year is in the dictionary, then I just want to set the item in the list to equal the value that month. My question is how do I access and set a specific item in a list within a dictionary. I am running into a number of errors which are not particularly helpful to google.
here is my code:
yeardict = {}
for key in sorted(monthdict):
dyear = str(key)[0:4]
dmonth = str(key)[5:]
output += "year: "+dyear+" month: "+dmonth
if dyear in yeardict:
pass
# yeardict[str(key)[0:4]][str(key)[5:]]=monthdict(key)
else:
yeardict[str(key)[0:4]]=[0,0,0,0,0,0,0,0,0,0,0,0]
# yeardict[int(dyear)][int(dmonth)]=monthdict(key)
The two lines that are commented out are where I want to actually set the values, and they introduce one of two errors when I add them to my code: 1. 'dict' is not callable 2. KeyError: 2009
Let me know if I can clarify anything. Thank you for looking.