[Python-Dev] tally (and other accumulators) (original) (raw)
Raymond Hettinger python at rcn.com
Tue Apr 4 20:26:17 CEST 2006
- Previous message: [Python-Dev] tally (and other accumulators)
- Next message: [Python-Dev] tally (and other accumulators)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[Alex]
This is quite general and simple at the same time: for example, it was proposed originally to answer some complaint about any and all giving no indication of the count of true/false items:
tally(bool(x) for x in seq) would give a dict with two entries, counts of true and false items.
FWIW, sum() works nicely for counting true entries:
>>> sum(x%3==0 for x in range(100))
34
Raymond
- Previous message: [Python-Dev] tally (and other accumulators)
- Next message: [Python-Dev] tally (and other accumulators)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]