Re: groupby behaviour

2013-02-26 Thread Paul Rubin
andrea crotti writes: > It's very weird though this sharing and still doesn't really look > rightl, is it done just for performance reasons? It could consume unbounded amounts of memory otherwise. E.g. if there are millions of items in the group. If you're not worried about that situation it's

Re: groupby behaviour

2013-02-26 Thread andrea crotti
2013/2/26 Ian Kelly : > On Tue, Feb 26, 2013 at 9:27 AM, andrea crotti > wrote: >> So I was trying to use groupby (which I used in the past), but I >> noticed a very strange thing if using list on >> the result: > > As stated in the docs: > > """ > The returned group is itself an iterator that sha

Re: groupby behaviour

2013-02-26 Thread Ian Kelly
On Tue, Feb 26, 2013 at 9:27 AM, andrea crotti wrote: > So I was trying to use groupby (which I used in the past), but I > noticed a very strange thing if using list on > the result: As stated in the docs: """ The returned group is itself an iterator that shares the underlying iterable with grou

Re: groupby - summing multiple columns in a list of lists

2011-05-17 Thread Peter Otten
Jackson wrote: > I'm currently using a function pasted in below. This allows me to sum > a column (index) in a list of lists. > > So if mylist = [[1, 2, 3], [1, 3, 4], [2, 3, 4], [2, 4, 5]] > group_results(mylist,[0],1) > > Returns: > [(1, 5), (2, 7)] > > What I would like to do is allow a tupl

Re: groupby() seems slow

2007-10-16 Thread Raymond Hettinger
On Oct 15, 8:02 pm, 7stud <[EMAIL PROTECTED]> wrote: > t = timeit.Timer("test3()", "from __main__ import test3, key, data") > print t.timeit() > t = timeit.Timer("test1()", "from __main__ import test1, data") > print t.timeit() > > --output:--- > 42.791079998 > 19.0128788948 > > I thought groupby()

Re: groupby() seems slow

2007-10-15 Thread George Sakkis
On Oct 15, 11:02 pm, 7stud <[EMAIL PROTECTED]> wrote: > I'm applying groupby() in a very simplistic way to split up some data, > but when I timeit against another method, it takes twice as long. The > following groupby() code groups the data between the "" strings: > > data = [ > "1.5","","2.5","3

Re: groupby and itemgetter

2006-10-06 Thread Steven Bethard
Roman Bertle wrote: > Hello, > > there is an example how to use groupby in the itertools documentation > (http://docs.python.org/lib/itertools-example.html): > > # Show a dictionary sorted and grouped by value from operator import itemgetter d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) >

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

Re: groupby

2006-05-27 Thread Paul McGuire
"Paul McGuire" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > So here's how to save the values from the iterators while iterating over the > groupby: > > >>> m = [(x,list(y)) for x,y in groupby([1, 1, 1, 2, 2, 3])] > >>> m > [(1, [1, 1, 1]), (2, [2, 2]), (3, [3])] > > -- Paul > > Pl

Re: groupby

2006-05-22 Thread Paul McGuire
"Bryan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > George Sakkis wrote: > > "The returned group is itself an iterator that shares the underlying > > iterable with groupby(). Because the source is shared, when the groupby > > object is advanced, the previous group is no longer vi

Re: groupby

2006-05-22 Thread Bryan
George Sakkis wrote: > Bryan wrote: > >> can some explain why in the 2nd example, m doesn't print the list [1, 1, 1] >> which i had expected? >> >> >> >>> for k, g in groupby([1, 1, 1, 2, 2, 3]): >> ... print k, list(g) >> ... >> 1 [1, 1, 1] >> 2 [2, 2] >> 3 [3] >> >> >> >>> m = list(groupby

Re: groupby

2006-05-22 Thread George Sakkis
Bryan wrote: > can some explain why in the 2nd example, m doesn't print the list [1, 1, 1] > which i had expected? > > > >>> for k, g in groupby([1, 1, 1, 2, 2, 3]): > ... print k, list(g) > ... > 1 [1, 1, 1] > 2 [2, 2] > 3 [3] > > > >>> m = list(groupby([1, 1, 1, 2, 2, 3])) > >>> m > [(1,