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
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
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
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
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()
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
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)
>
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)
>
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
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
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
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
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
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
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
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
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)
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
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
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
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
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
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
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
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]
>
> 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:
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
"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
"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
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
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,
31 matches
Mail list logo