How to efficently build a nested dictionary
Carsten Gaebler
cg at schlund.de
Sat Mar 10 13:36:48 EST 2001
Carsten Gaebler wrote:
>
> Aahz Maruch wrote:
> > >
> > >traffic = {dir: {year: {month: {day: 42}}}}
> >
> > I think Emile's suggestion to use a single compound key is a good one.
> > If that's not sufficient *and* you can use Python 2.0, take a look at
> > the dict.setdefault() method.
>
> How could the setdefault() method help me out?
By just saying
traffic[dir][year][month][day] =
traffic.setdefault(dir,{}).setdefault(year,{}).setdefault(month,{}).setdefault(day,
0) + bytes
Yes, I know, sometimes I'm a bit slow ... :-)
cg.
More information about the Python-list
mailing list