Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin.stu.neva.ru!news2.euro.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'true,': 0.05; 'memory.': 0.07; 'subject:two': 0.07; '[],': 0.09; 'iterate': 0.09; 'subject:into': 0.09; 'cc:addr:python-list': 0.11; '>>': 0.16; '[none]': 0.16; 'discarded.': 0.16; 'expression.': 0.16; 'iterator': 0.16; 'lambda': 0.16; 'path:': 0.16; 'roy': 0.16; 'smith"': 0.16; 'subject:based': 0.16; '\xc2\xa0i': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'thanks.': 0.20; 'code,': 0.22; 'memory': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'rid': 0.24; 'cheers,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; 'pass': 0.26; 'least': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'skip:& 60': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; '(although': 0.31; 'constant': 0.31; 'figure': 0.32; 'url:python': 0.33; 'continuing': 0.33; 'could': 0.34; 'created': 0.35; 'requirement': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'useful': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'list': 0.37; 'skip:o 20': 0.38; '8bit%:86': 0.38; 'anything': 0.39; 'sure': 0.39; 'url:mail': 0.40; 'course': 0.61; 'kind': 0.63; 'more': 0.64; 'smith': 0.68; 'article': 0.77; 'phil': 0.84; 'received:mail-ob0-x22f.google.com': 0.84; 'usage.': 0.84; 'technique': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=FwfI4B7vHQiXXYi1e5SCYGLdOzPSMpxyrJvK4eDj1iQ=; b=EAQ4e9Q+/Qq9O/tJxbt71ZtqBQIyTHzdJcMPcR8815bDSQxdAf0U8XyF3eJ0yUzU3l UqVQLlA5s2S7rwc00+c2ohsXm5ZGOXHugpaDv9o9GmiVBtIp5JZ+wAoh+LW4fL4/dvcv 2sXcmAzbh+4E9HKBGpAwrdJoTwrl8rwv8AIZ225JxW1KXvhQ/Vb6cVSMhcQWNq6r7ONU VDAWBImLmUs7rTrFbnYtyWaotTDzDw30DqxjU4dn5iN0eaDSByJK4mPH8YdpdRsI1ZVD ZEmtMNyw2jbHYXdt+zQvf7eGTU6uDBVXjM9ogSzmjeUoJ0/87TIZ9lTb02zFiqXGhag7 qihQ== MIME-Version: 1.0 X-Received: by 10.60.143.41 with SMTP id sb9mr7142394oeb.102.1371018746403; Tue, 11 Jun 2013 23:32:26 -0700 (PDT) In-Reply-To: References: Date: Wed, 12 Jun 2013 07:32:26 +0100 Subject: Re: Split a list into two parts based on a filter? From: Phil Connell To: Roy Smith Content-Type: multipart/alternative; boundary=047d7b3a89fc5c6d4104deef2d53 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 113 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1371018755 news.xs4all.nl 15930 [2001:888:2000:d::a6]:51365 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47743 --047d7b3a89fc5c6d4104deef2d53 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 12 Jun 2013 01:36, "Roy Smith" wrote: > > In article , > Serhiy Storchaka wrote: > > > 11.06.13 07:11, Roy Smith =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=B2(= =D0=BB=D0=B0): > > > In article , > > > Roel Schroeven wrote: > > > > > >> new_songs, old_songs =3D [], [] > > >> [(new_songs if s.is_new() else old_songs).append(s) for s in songs] > > > > > > Thanks kind of neat, thanks. > > > > > > I'm trying to figure out what list gets created and discarded. I think > > > it's [None] * len(songs). > > > > It is the same as your klunky code, but consumes more memory. > > Well, continuing down this somewhat bizarre path: > > new_songs, old_songs =3D [], [] > itertools.takewhile( > lambda x: True, > (new_songs if s.is_new() else old_songs).append(s) for s in songs) > ) > > I'm not sure I got the syntax exactly right, but the idea is anything > that will iterate over a generator expression. That at least gets rid > of the memory requirement to hold the throw-away list :-) You could equivalently pass the generator to deque() with maxlen=3D0 - this consumes the iterator with constant memory usage. We are of course firmly in the twilight zone at this point (although this can be a useful technique in general). Cheers, Phil > > -- > http://mail.python.org/mailman/listinfo/python-list > --047d7b3a89fc5c6d4104deef2d53 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


On 12 Jun 2013 01:36, "Roy Smith" <roy@panix.com> wrote:
>
> In article <mailman.3023.1370964449.3114.python-list@python.org>= ,
> =C2=A0Serhiy Storchaka <stor= chaka@gmail.com> wrote:
>
> > 11.06.13 07:11, Roy Smith =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0= =B2(=D0=BB=D0=B0):
> > > In article <mailman.2992.1370904643.3114.python-list@python.o= rg>,
> > > =C2=A0 Roel Schroeven <roel@roelschroeven.net> wrote:
> > >
> > >> new_songs, old_songs =3D [], []
> > >> [(new_songs if s.is_new() else old_songs).append(s) for = s in songs]
> > >
> > > Thanks kind of neat, thanks.
> > >
> > > I'm trying to figure out what list gets created and disc= arded. =C2=A0I think
> > > it's [None] * len(songs).
> >
> > It is the same as your klunky code, but consumes more memory.
>
> Well, continuing down this somewhat bizarre path:
>
> new_songs, old_songs =3D [], []
> itertools.takewhile(
> =C2=A0 =C2=A0 lambda x: True,
> =C2=A0 =C2=A0 (new_songs if s.is_new() else old_songs).append(s) for s= in songs)
> =C2=A0 =C2=A0 )
>
> I'm not sure I got the syntax exactly right, but the idea is anyth= ing
> that will iterate over a generator expression. =C2=A0That at least get= s rid
> of the memory requirement to hold the throw-away list :-)

You could equivalently pass the generator to deque() with ma= xlen=3D0 - this consumes the iterator with constant memory usage.

We are of course firmly in the twilight zone at this point (= although this can be a useful technique in general).

Cheers,
Phil

>
> --
> http:/= /mail.python.org/mailman/listinfo/python-list
>

--047d7b3a89fc5c6d4104deef2d53--