Nested dictionaries trouble

7stud bbxx789_05ss at yahoo.com
Wed Apr 11 16:56:14 EDT 2007


1) You have this setup:

    logMonths = {"Jan":"01", "Feb":"02",...}
    yearTotals = {
        "2005":{"01":0, "02":0, ....}
        "2006":
        "2007":
    }

Then when you get a value such as "Jan", you look up the "Jan" in the
logMonths dictionary to get "01".  Then you use "01" and the year, say
"2005", to look up the value in the yearTotals dictionary.  Why do
that?  What is the point of even having the logMonths dictionary?  Why
not make "Jan" the key in the the "2005" dictionary and look it up
directly:

yearTotals = {
"2005":{"Jan":0, "Feb":0, ....}
"2006":
"2007":
}

That way you could completely eliminate the lookup in the logMonths
dict.

2) In this part:

    logMonth = logMonths[logLine[1]]
    currentYearMonth = yearTotals[logLine[4]][logMonth]
    # Update year/month value
    currentYearMonth += int(logLine[7])
    yearTotals[logLine[4]][logMonth] = currentYearMonth

I'm not sure why you are using all those intermediate steps.  How
about:

    yearTotals[logLine[4]][logLine[1]] += int(logLine[7])

To me that is a lot clearer.  Or, you could do this:

    year, month, val = logLine[4], logLine[1], int(logLine[7])
    yearTotals[year][month] += val

3)
>I'm thinking there's an error in the way
>I set my dictionaries up or reference them

Yep.  It's right here:

for year in years:
  yearTotals.setdefault(year, monthTotals)

Every year refers to the same monthTotals dict.  You can use a dicts
copy() function to make a copy:

monthTotals.copy()

Here is a reworking of your code that also eliminates a lot of typing:

import calendar, pprint

years = ["200%s" % x for x in range(5, 8)]
print years

months = list(calendar.month_abbr)
print months

monthTotals = dict.fromkeys(months[1:], 0)
print monthTotals

yearTotals = {}
for year in years:
    yearTotals.setdefault(year, monthTotals.copy())
pprint.pprint(yearTotals)

logs = [
["", "Feb", "", "", "2007", "", "", "12"],
["", "Jan", "", "", "2005", "", "", "3"],
["", "Jan", "", "", "2005", "", "", "7"],
]

for logLine in logs:
    year, month, val = logLine[4], logLine[1], int(logLine[7])
    yearTotals[year][month] += val

for x in yearTotals.keys():
    print "KEY", "\t", "VALUE"
    print x, "\t", yearTotals[x]
    for y in yearTotals[x].keys():
        print "   ", y, "\t", yearTotals[x][y]




More information about the Python-list mailing list