Pre-PEP: Dictionary accumulator methods

Paul McGuire ptmcg at austin.rr.com
Sat Mar 19 11:07:14 EST 2005


-1 on set increment.
I think this makes your intent much clearer:

.d={}
.for word in text.split():
.    d.tally(word)
.    if word.lower() in ["a","an","the"]:
.        d.tally(word,-1)

or perhaps simplest:

.d={}
.for word in text.split():
.    if word.lower() not in ["a","an","the"]:
.        d.tally(word)

Personally, I'm +1 for tally(), and possibly tallyList() and tallySet()
to complete the thought for the cumulative container cases.  I think
there is something to be gained if these methods get named in some
similar manner.

For those dead set against tally() and its ilk, how about accum(),
accumList() and accumSet()?

-- Paul




More information about the Python-list mailing list