"groupby" is brilliant!

John Machin sjmachin at lexicon.net
Tue Jun 13 17:58:50 EDT 2006


On 13/06/2006 6:28 PM, Paul McGuire wrote:

> (Oh, and I like groupby too!  Combine it with sort to quickly create
> histograms.)
> 
> # tally a histogram of a list of values from 1-10
> dataValueRange = range(1,11)
> data = [random.choice(dataValueRange) for i in xrange(10000)]
> 
> hist = [ (k,len(list(g))) for k,g in itertools.groupby(sorted(data)) ]

That len(list(g)) looks like it uses O(N) memory just to find out what N 
is :-(

The best I could come up with is sum(itertools.imap(lambda x: 1, g)) -- 
but that does look a bit ugly ...




More information about the Python-list mailing list