[Tutor] Ok, take one step back and let you look
Moshe Zadka
Moshe Zadka <mzadka@geocities.com>
Fri, 1 Oct 1999 10:59:50 +0200 (IST)
On Fri, 1 Oct 1999, Moshe Zadka wrote:
> On Thu, 30 Sep 1999, Michael P. Reilly wrote:
>
> > >>> classify = {}
> > >>> for item in inventory:
> > ... key = item.__class__
> > ... if classify.has_key(key):
> > ... classify[key] = classify[key] + 1
> > ... else:
> > ... classify[key] = 1
>
> But a better idiom, both in clarity and efficiency, would be to simulate
> Perl's hash semantics and use .get:
>
> classify={}
> for item in inventory:
> classify[item.__class__]=classify.get(item.__class__, 0)
Sorry for the typo. There should be a +1 at the end of the last line.
--
Moshe Zadka <mzadka@geocities.com>.
INTERNET: Learn what you know.
Share what you don't.