Home

Sion Arrowsmith <siona@chiark.greenend.org.uk> wrote:

> special_dragonfly <Dominic@PLEASEASK.co.uk> wrote:
> > if key in FieldsDictionary:
> > FieldsDictionary[key].append(FieldClass(*line.split(",")))
> > else:
> > FieldsDictionary[key]=[FieldClass(*line.split(","))]
>
> These four lines can be replaced by:
>
> FieldsDictionary.setdefault(key, []).append(FieldClass(*line.split(",")))

Even better might be to let FieldsDictionary be an instance of
collections.defaultdict(list) [[assuming Python 2.5 is in use]], in
which case the simpler

FieldsDictionary[key].append(FieldClass(*line.split(",")))

will Just Work. setdefault was a valiant attempt at fixing this
problem, but defaultdict is better.


Alex

previous
next

Re: Puzzled by "is"
Re: How to determine the bool between the strings and ints?
ActiveX control in python vs ActiveX control in vb 6 (piece of code)
Re: Nested template specialization?
type compatibility warnings -- building python on windows 64-bit x64
Fundacja Hobbit
Dzieci Niczyje
Akogo
Pajacyk
Rodzic Po Ludzku