"groupby" is brilliant!

Robert Kern robert.kern at gmail.com
Tue Jun 13 18:38:22 EDT 2006


Gary Herron wrote:
> John Machin wrote:
> 
>>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 :-(
> 
> Not at all! A python list *knows* its length at all times. len() is a
> constant time lookup of an internal attribute.

The point is that you had to create the list in the first place. g is an iterator.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list