Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39978

Re: groupby behaviour

From Paul Rubin <no.email@nospam.invalid>
Newsgroups comp.lang.python
Subject Re: groupby behaviour
Date 2013-02-26 09:35 -0800
Organization Nightsong/Fort GNOX
Message-ID <7xhakzvuzp.fsf@ruckus.brouhaha.com> (permalink)
References <CAF_E5JasO841FBst9EtXJJEQkK54UELgBcAZk8GVyGHigxu8TQ@mail.gmail.com> <CALwzidkatUmAyR06VwdqBr1MH=AbeNhxwzMufkREo3SRa_AbLg@mail.gmail.com> <mailman.2557.1361898573.2939.python-list@python.org>

Show all headers | View raw


andrea crotti <andrea.crotti.0@gmail.com> 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 simplest to just
convert each group to a list for processing it:

   for k,g in groupby(...):
      gs = list(g)
      # do stuff with gs

calling list(g) reads all the elements in the group, advancing groupby's
internal iterator to the next group.  Since you've copied out all the
group elements, you don't have to worry about the sharing effect.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: groupby behaviour andrea crotti <andrea.crotti.0@gmail.com> - 2013-02-26 17:09 +0000
  Re: groupby behaviour Paul Rubin <no.email@nospam.invalid> - 2013-02-26 09:35 -0800

csiph-web