How to initialize a table of months.

Jun.Jin.act+group.python@gmail.com Jun.Jin.act at gmail.com
Mon Apr 16 02:08:29 EDT 2007


On Apr 16, 1:14 pm, "Paul McGuire" <p... at austin.rr.com> wrote:
> On Apr 15, 10:33 pm, "7stud" <bbxx789_0... at yahoo.com> wrote:
>
>
>
> > On Apr 15, 9:30 pm, "7stud" <bbxx789_0... at yahoo.com> wrote:
>
> > > On Apr 15, 7:30 pm, "Steven W. Orr" <ste... at syslang.net> wrote:
>
> > Arrgh.
>
> > import calendar
>
> > months = calendar.month_abbr
> > #returns an array with the 0 element empty
> > #so the month names line up with the indexes 1-12
>
> > d = {}
> > for i in range(1, 13):
> >     d[months[i]] = i
>
> > print d
>
> This dict construction idiom is worth learning:
> d = dict( (a,b) for a,b in ... some kind of list comprehension or
> generator expr... )
>
> In this case:
> d = dict( (mon,i) for i,mon in enumerate(calendar.month_abbr) )
>
> Or to avoid including that pesky 0'th blank element:
> d = dict( [(mon,i) for i,mon in enumerate(calendar.month_abbr)][1:] )
>
> -- Paul

Great!




More information about the Python-list mailing list