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


Groups > comp.lang.python > #60761

Re: For-each behavior while modifying a collection

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.056
X-Spam-Evidence '*H*': 0.89; '*S*': 0.00; 'bits': 0.09; 'clean.': 0.09; 'subject:while': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'be:': 0.16; 'bits.': 0.16; 'comp': 0.16; 'enough.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'visible': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'references': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'usually': 0.31; 'are.': 0.31; 'fri,': 0.33; 'actual': 0.34; 'done.': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'should': 0.36; 'list': 0.37; 'step': 0.37; 'nov': 0.38; 'little': 0.38; 'length': 0.61; 'profile': 0.61; "you're": 0.61; "you'll": 0.62; 'more': 0.64; 'between': 0.67; 'importantly,': 0.68; 'subject:For': 0.78; '"fast"': 0.84; 'duplication,': 0.84; 'seldom': 0.84; 'to:none': 0.92; '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:cc :content-type; bh=u0hkVTNt2YS1tQOcEaoUwJjZrJuHLHkoRY2XkJMZSb0=; b=SWD4mGXNa3W26+KEPodWAQZPAgrYOsWd17/fNq4NyKHfHvDMI97ARsBzHv3ASehG4U m5mDg/tExS8FxLOsbGkK3uZ8gm3s0Pvrd28dQRvU2p5mT7uIBB5RtgAWTmYdjogNnXFA PzzsLyRrgm5Os9I++9XNrED9X+97LbfsN2ZO4XAeE/lEcgdqjZNyfRbc79MTtyJ96LJI 0uvADe8RHrk2M14i+7NAisNKkRm4raCl5Eds5kGjiENOePYNEzGHtxQHITIZVq8poH4+ aFC5KBtclNNTdwQNdPTDrET0V9/KtTPOV5iDYDZvhPfaPrTlAl7V8NiDbjzmkSIOwmQO 3bQQ==
MIME-Version 1.0
X-Received by 10.68.225.9 with SMTP id rg9mr15328643pbc.122.1385718473482; Fri, 29 Nov 2013 01:47:53 -0800 (PST)
In-Reply-To <CAO5g_ee7qCLrrJ8ir13nLF1S=HPPDhawRAeDjN3vgstPcG-psg@mail.gmail.com>
References <CAO5g_efLUD9kJ_W+fBUWv=9yrDggN2560H5Fx3uUVDQ=oAVREg@mail.gmail.com> <l77u09$30r$1@ger.gmane.org> <52977C71.4010200@mrabarnett.plus.com> <CAO5g_ee7qCLrrJ8ir13nLF1S=HPPDhawRAeDjN3vgstPcG-psg@mail.gmail.com>
Date Fri, 29 Nov 2013 20:47:53 +1100
Subject Re: For-each behavior while modifying a collection
From Chris Angelico <rosuav@gmail.com>
Cc python-list@python.org
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3395.1385718481.18130.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385718481 news.xs4all.nl 15908 [2001:888:2000:d::a6]:60697
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60761

Show key headers only | View raw


On Fri, Nov 29, 2013 at 9:14 AM, Valentin Zahnd <v.zahnd@gmail.com> wrote:
>      def keepByValue(self, key=None, value=[]):
>          tmpFlows = []
>          while len(self.flows) > 0:
>              row = self.flows.pop()
>              if row[key] in value:
>                  tmpFlows.append(row)
>          self.flows = tmpFlows

This is almost certainly going to be less efficient than the
comprehension - it churns the length of the list terribly. There's
very little duplication, as the actual _contents_ will not be
duplicated, only references to them; so the list comp is both clean
and efficient.

More importantly, the way you write a Python program should be:

1) Make it work and look clean.
2) See if it's fast enough. If so - and it usually will be - you're done.
3) Profile it and find out where the slow bits really are.
4) Try to speed up some of the slow bits.

You'll seldom get past step 2. The difference between "fast" and "fast
enough" is usually not visible to a human.

ChrisA

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


Thread

Re: For-each behavior while modifying a collection Chris Angelico <rosuav@gmail.com> - 2013-11-29 20:47 +1100

csiph-web