Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.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.041 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'subject:two': 0.07; '[1,': 0.09; 'subject:into': 0.09; 'sure,': 0.09; 'thrown': 0.09; 'ah,': 0.16; 'algorithmic': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hmm.': 0.16; 'low.': 0.16; 'roy': 0.16; 'subject:based': 0.16; 'there...': 0.16; 'uniquely': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'meant': 0.20; '>>>': 0.22; 'affects': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'forgot': 0.30; 'relative': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'getting': 0.31; '>>>>': 0.31; 'another': 0.32; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'subject:?': 0.36; 'skip:o 20': 0.38; '8bit%:86': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'numbers': 0.61; 'complete': 0.62; 'costs': 0.63; 'smith': 0.68; 'fail.': 0.84; 'no:': 0.84; 'otten': 0.84; 'ridiculously': 0.84; 'absolutely': 0.87; 'notion': 0.91; 'songs': 0.91; 'choice.': 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 :content-type:content-transfer-encoding; bh=1/gD+6kvOey2DJj9wWKoTeet6czITClckxG86s1FBrE=; b=GHEgTNsIhM7ctvq1tfvwugY69/TmQhP7+wDaGdiqoQtfr9ONlfqGVdCM2ihwuRvtDy nvwaPeGiKal6axKZqEGphSfOAYWdhRm0lOYaQ3XkMf6avn44q8/YcbB20iEOxV0arsCt WZcW8NQP65cxgVAYkNP1d6WpmVjNDHdMH+YWIORTVcphX3WDK42+w9OsFKzxt4lOYTq7 dMtgUOeA3kgb898YVVyeSOQO4+h+O2RXIFLqL/JHOE4xcXNBF6y7SHzYl7j9gTqvWH++ tSnN6uTEuaEGY7xbUuPwXTyqi3FoOKf8xY8FwicoM+greZfI35Ozxo7/jgJ8AsGDgmBy soig== MIME-Version: 1.0 X-Received: by 10.52.117.16 with SMTP id ka16mr7019260vdb.43.1370975221770; Tue, 11 Jun 2013 11:27:01 -0700 (PDT) In-Reply-To: References: Date: Wed, 12 Jun 2013 04:27:01 +1000 Subject: Re: Split a list into two parts based on a filter? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370975224 news.xs4all.nl 15969 [2001:888:2000:d::a6]:56024 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47694 On Wed, Jun 12, 2013 at 4:13 AM, Peter Otten <__peter__@web.de> wrote: > Chris Angelico wrote: > >> On Wed, Jun 12, 2013 at 1:28 AM, Serhiy Storchaka >> wrote: >>> 11.06.13 01:50, Chris Angelico =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0= =B2(=D0=BB=D0=B0): >>> >>>> On Tue, Jun 11, 2013 at 6:34 AM, Roy Smith wrote: >>>>> >>>>> new_songs =3D [s for s in songs if s.is_new()] >>>>> old_songs =3D [s for s in songs if not s.is_new()] >>>> >>>> >>>> Hmm. Would this serve? >>>> >>>> old_songs =3D songs[:] >>>> new_songs =3D [songs.remove(s) or s for s in songs if s.is_new()] > > I think you meant old_songs.remove(s). Ah, yes, editing fail. I started by mutating the original list, then thought "Oh, better to work with a copy"... and forgot to complete the edit. >>> O(len(songs)**2) complexity. >> >> Which isn't significant if len(songs) is low. We weren't told the >> relative costs - is the is_new call ridiculously expensive? Everything >> affects algorithmic choice. > > But is it correct? In the general case, no: > >>>> numbers =3D [1, 1.0, 2.0, 2] >>>> ints =3D numbers[:] >>>> floats =3D [ints.remove(n) or n for n in numbers if isinstance(n, floa= t)] >>>> floats > [1.0, 2.0] >>>> ints > [1.0, 2] # hmm Sure, but the implication of the original is that they're uniquely identifiable. Anyway, it wasn't meant to be absolutely perfect, just another notion getting thrown out there... and then thrown out :) ChrisA