Re: [Python-Dev] tally (and other accumulators)

2006-04-05 Thread Greg Ewing
Jess Austin wrote: > I'll go > so far as to suggest that the existence of groupby() obviates the > proposed tally(). Except that it requires building a list of values in each group when all you want at the end is the length of the list. -- Greg ___ Pyth

Re: [Python-Dev] tally (and other accumulators)

2006-04-05 Thread Scott David Daniels
Jess Austin wrote: > Alex wrote: >> On Apr 4, 2006, at 10:53 PM, Jess Austin wrote: >>> Alex wrote: import collections def tally(seq): d = collections.defaultdict(int) for item in seq: d[item] += 1 return dict(d) [Jess again] >>> def tally(seq):

Re: [Python-Dev] tally (and other accumulators)

2006-04-04 Thread Jess Austin
Alex wrote: > On Apr 4, 2006, at 10:53 PM, Jess Austin wrote: > > Alex wrote: > >> import collections > >> def tally(seq): > >> d = collections.defaultdict(int) > >> for item in seq: > >> d[item] += 1 > >> return dict(d) > > > > I'll stop lurking and submit the following: >

Re: [Python-Dev] tally (and other accumulators)

2006-04-04 Thread Alex Martelli
On Apr 4, 2006, at 10:53 PM, Jess Austin wrote: > Alex wrote: >> import collections >> def tally(seq): >> d = collections.defaultdict(int) >> for item in seq: >> d[item] += 1 >> return dict(d) > > I'll stop lurking and submit the following: > > def tally(seq): > return

Re: [Python-Dev] tally (and other accumulators)

2006-04-04 Thread Jess Austin
Alex wrote: > import collections > def tally(seq): > d = collections.defaultdict(int) > for item in seq: > d[item] += 1 > return dict(d) I'll stop lurking and submit the following: def tally(seq): return dict((group[0], len(tuple(group[1]))) for group

Re: [Python-Dev] tally (and other accumulators)

2006-04-04 Thread Alex Martelli
On 4/4/06, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > [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 se

Re: [Python-Dev] tally (and other accumulators)

2006-04-04 Thread Raymond Hettinger
[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

Re: [Python-Dev] tally (and other accumulators)

2006-04-04 Thread Georg Brandl
Alex Martelli wrote: > On Apr 4, 2006, at 8:01 AM, Jeremy Hylton wrote: > >> On 4/4/06, Alex Martelli <[EMAIL PROTECTED]> wrote: >>> import collections >>> def tally(seq): >>> d = collections.defaultdict(int) >>> for item in seq: >>> d[item] += 1 >>> return dict(d) > ..

Re: [Python-Dev] tally (and other accumulators)

2006-04-04 Thread Ian Bicking
Alex Martelli wrote: > It's a bit late for 2.5, of course, but, I thought I'd propose it > anyway -- I noticed it on c.l.py. > > In 2.3/2.4 we have many ways to generate and process iterators but > few "accumulators" -- functions that accept an iterable and produce > some kind of "summary re

Re: [Python-Dev] tally (and other accumulators)

2006-04-04 Thread Alex Martelli
On Apr 4, 2006, at 8:01 AM, Jeremy Hylton wrote: > On 4/4/06, Alex Martelli <[EMAIL PROTECTED]> wrote: >> import collections >> def tally(seq): >> d = collections.defaultdict(int) >> for item in seq: >> d[item] += 1 >> return dict(d) ... > Putting it somewhere in colle

Re: [Python-Dev] tally (and other accumulators)

2006-04-04 Thread Jeremy Hylton
On 4/4/06, Alex Martelli <[EMAIL PROTECTED]> wrote: > import collections > def tally(seq): > d = collections.defaultdict(int) > for item in seq: > d[item] += 1 > return dict(d) > > Nevertheless, simplicity and generality make it advisable to supply > it as part of the standa