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


Groups > comp.lang.python > #39978

Re: groupby behaviour

Path csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!.POSTED!not-for-mail
From Paul Rubin <no.email@nospam.invalid>
Newsgroups comp.lang.python
Subject Re: groupby behaviour
Date Tue, 26 Feb 2013 09:35:22 -0800
Organization Nightsong/Fort GNOX
Lines 17
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>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Info mx05.eternal-september.org; posting-host="d94d289a4df6ae47ea4d4f8b2ae808e7"; logging-data="27473"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ncHesm/3vYtSEvNuUBt1R"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)
Cancel-Lock sha1:JvrDSKOoPdRTfRLNTBoS0rwLNyA= sha1:llZ9VwAk6XuiZksylrl4eecwpo8=
Xref csiph.com comp.lang.python:39978

Show key headers only | 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