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


Groups > comp.lang.python > #40188

Re: suggestions for improving code fragment please

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'repeated': 0.07; 'subject:code': 0.07; 'try:': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'indexerror:': 0.16; 'popping': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'subject:please': 0.27; 'header:X -Complaints-To:1': 0.28; "i'm": 0.29; 'to:addr:python-list': 0.33; 'front': 0.33; 'pm,': 0.35; 'there': 0.35; 'received:org': 0.36; 'except': 0.36; 'should': 0.36; 'being': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'night': 0.62; 'removal': 0.65; 'dumb': 0.84; 'received:fios.verizon.net': 0.84; 'same,': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: suggestions for improving code fragment please
Date Thu, 28 Feb 2013 16:30:36 -0500
References <u8idnaoktKrcKbLMnZ2dnUVZ8uednZ2d@brightview.co.uk>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130215 Thunderbird/17.0.3
In-Reply-To <u8idnaoktKrcKbLMnZ2dnUVZ8uednZ2d@brightview.co.uk>
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2686.1362087056.2939.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1362087056 news.xs4all.nl 6969 [2001:888:2000:d::a6]:52714
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:40188

Show key headers only | View raw


On 2/28/2013 2:47 PM, The Night Tripper wrote:
> Hi there
>      I'm being very dumb ... how can I simplify this fragment?
>
>
>          if arglist:
>              arglist.pop(0)
>              if arglist:
>                  self.myparm1 = arglist.pop(0)
>                  if arglist:
>                      self.myparm2 = arglist.pop(0)
>                      if arglist:
>                          self.myparm3 = arglist.pop(0)
>                          if arglist:
>                              self.parm4 = arglist.pop(0)

To literally do the same thing

try:
     arglist.pop(0)
     self.myparm1 = arglist.pop(0)
     self.myparm2 = arglist.pop(0)
     self.myparm3 = arglist.pop(0)
     self.parm4 = arglist.pop(0)
except IndexError:
     pass

However, repeated popping from the front is O(n**2) instead of O(n).
Following should do the same, including removal from original arglist.

it = iter(arglist)
n = 0
try:
     next(it); n += 1
     self.myparm1 = next(it); n += 1
     self.myparm2 = next(it); n += 1
     self.myparm3 = next(it); n += 1
     self.parm4 = next(it); n += 1
except StopIteration:
     pass
arglist = arglist[n:]

-- 
Terry Jan Reedy

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


Thread

suggestions for improving code fragment please The Night Tripper <jkn+gg@nicorp.co.uk> - 2013-02-28 19:47 +0000
  Re: suggestions for improving code fragment please Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-28 11:55 -0800
  Re: suggestions for improving code fragment please Joel Goldstick <joel.goldstick@gmail.com> - 2013-02-28 14:56 -0500
    Re: suggestions for improving code fragment please The Night Tripper <jkn+gg@nicorp.co.uk> - 2013-02-28 23:20 +0000
  Re: suggestions for improving code fragment please Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-28 12:58 -0700
  Re: suggestions for improving code fragment please Tim Chase <python.list@tim.thechases.com> - 2013-02-28 14:37 -0600
  Re: suggestions for improving code fragment please MRAB <python@mrabarnett.plus.com> - 2013-02-28 21:13 +0000
  Re: suggestions for improving code fragment please Mitya Sirenef <msirenef@lightbird.net> - 2013-02-28 16:22 -0500
  Re: suggestions for improving code fragment please Dave Angel <davea@davea.name> - 2013-02-28 16:28 -0500
  Re: suggestions for improving code fragment please Terry Reedy <tjreedy@udel.edu> - 2013-02-28 16:30 -0500
  Re: suggestions for improving code fragment please Tim Chase <python.list@tim.thechases.com> - 2013-02-28 16:44 -0600
  Re: suggestions for improving code fragment please Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-01 02:44 +0000

csiph-web