Re: "groupby" is brilliant!

2006-06-14 Thread Alex Martelli
James Stroud <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > James Stroud <[EMAIL PROTECTED]> wrote: > >... > > > >>def doit(rows, doers, i=0): > >> for r, alist in groupby(rows, itemgetter(i)): > >> if len(doers) > 1: > >> doit(alist, doers[1:], i+1) > >> doers[0](r) >

Re: "groupby" is brilliant!

2006-06-14 Thread James Stroud
Alex Martelli wrote: > James Stroud <[EMAIL PROTECTED]> wrote: >... > >>def doit(rows, doers, i=0): >> for r, alist in groupby(rows, itemgetter(i)): >> if len(doers) > 1: >> doit(alist, doers[1:], i+1) >> doers[0](r) > > > Isn't this making N useless slices (thus copies, for

Re: "groupby" is brilliant!

2006-06-14 Thread James Stroud
Alex Martelli wrote: > James Stroud <[EMAIL PROTECTED]> wrote: >... > >>def doit(rows, doers, i=0): >> for r, alist in groupby(rows, itemgetter(i)): >> if len(doers) > 1: >> doit(alist, doers[1:], i+1) >> doers[0](r) > > > Isn't this making N useless slices (thus copies, for

Re: "groupby" is brilliant!

2006-06-14 Thread Alex Martelli
James Stroud <[EMAIL PROTECTED]> wrote: ... > def doit(rows, doers, i=0): >for r, alist in groupby(rows, itemgetter(i)): > if len(doers) > 1: >doit(alist, doers[1:], i+1) > doers[0](r) Isn't this making N useless slices (thus copies, for most kinds of sequences) for a doer

Re: "groupby" is brilliant!

2006-06-14 Thread Alex Martelli
Frank Millman <[EMAIL PROTECTED]> wrote: > Benji York wrote: > > Frank Millman wrote: > > > reader = csv.reader(open('trans.csv', 'rb')) > > > rows = [] > > > for row in reader: > > > rows.append(row) > > > > Why do you create a list of rows instead of just iterating over the > > reader direct

Re: "groupby" is brilliant!

2006-06-13 Thread Frank Millman
Benji York wrote: > Frank Millman wrote: > > reader = csv.reader(open('trans.csv', 'rb')) > > rows = [] > > for row in reader: > > rows.append(row) > > Why do you create a list of rows instead of just iterating over the > reader directly? > -- > Benji York A - didn't think of it - good idea

Re: "groupby" is brilliant!

2006-06-13 Thread Paul McGuire
John Machin" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > On 14/06/2006 8:38 AM, Robert Kern wrote: > > 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

Re: "groupby" is brilliant!

2006-06-13 Thread John Machin
On 14/06/2006 8:38 AM, Robert Kern wrote: > 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 dat

Re: "groupby" is brilliant!

2006-06-13 Thread Robert Kern
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(dataVa

Re: "groupby" is brilliant!

2006-06-13 Thread John Machin
On 14/06/2006 8:06 AM, 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)

Re: "groupby" is brilliant!

2006-06-13 Thread Gary Herron
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 xr

Re: "groupby" is brilliant!

2006-06-13 Thread John Machin
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(1)] > > hist = [ (k,le

Re: "groupby" is brilliant!

2006-06-13 Thread Jon Clements
Not related to itertools.groupby, but the csv.reader object... If for some reason you have malformed CSV files, with embedded newlines or something of that effect, it will raise an exception. To skip those, you will need a construct of something like this: raw_csv_in = file('filenamehere.csv') fo

Re: "groupby" is brilliant!

2006-06-13 Thread James Stroud
James Stroud wrote: > Frank Millman wrote: > >> Hi all >> >> This is probably old hat to most of you, but for me it was a >> revelation, so I thought I would share it in case someone has a similar >> requirement. >> >> I had to convert an old program that does a traditional pass through a >> sorte

Re: "groupby" is brilliant!

2006-06-13 Thread James Stroud
Frank Millman wrote: > Hi all > > This is probably old hat to most of you, but for me it was a > revelation, so I thought I would share it in case someone has a similar > requirement. > > I had to convert an old program that does a traditional pass through a > sorted data file, breaking on a chan

Re: "groupby" is brilliant!

2006-06-13 Thread Benji York
Frank Millman wrote: > reader = csv.reader(open('trans.csv', 'rb')) > rows = [] > for row in reader: > rows.append(row) Why do you create a list of rows instead of just iterating over the reader directly? -- Benji York -- http://mail.python.org/mailman/listinfo/python-list

Re: "groupby" is brilliant!

2006-06-13 Thread geskerrett
Frank; I would just like to thank-you for this timely post. I am working on a reporting project that needed "groupby" functionality and I was going to sit down this morning to rework some "very ugly code" into some "not quite so ugly code". Your post got me pointed to in the "right" direction and

Re: "groupby" is brilliant!

2006-06-13 Thread Frank Millman
Paul McGuire wrote: > > > > reader = csv.reader(open('trans.csv', 'rb')) > > rows = [] > > for row in reader: > > rows.append(row) > > > > This is untested, but you might think about converting your explicit "for... > append" loop into either a list comp, > > rows = [row for row in reader]

Re: "groupby" is brilliant!

2006-06-13 Thread Paul McGuire
> > reader = csv.reader(open('trans.csv', 'rb')) > rows = [] > for row in reader: > rows.append(row) > This is untested, but you might think about converting your explicit "for... append" loop into either a list comp, rows = [row for row in reader] or just a plain list constructor:

Re: "groupby" is brilliant!

2006-06-13 Thread vpr
Hi Frank This is one of the reasons why I love Python, you can write readable code. I strive to write clean code but I find that exception handling code e.g. try: makes my code ugly and significantly harder to read. Does anyone have any good pointers for a former C++ / Perl coder. /vpr Frank Mi

"groupby" is brilliant!

2006-06-12 Thread Frank Millman
Hi all This is probably old hat to most of you, but for me it was a revelation, so I thought I would share it in case someone has a similar requirement. I had to convert an old program that does a traditional pass through a sorted data file, breaking on a change of certain fields, processing each