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


Groups > comp.lang.python > #3270

Re: Pythonic infinite for loop?

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.linkpendium.com!news.linkpendium.com!panix!roy
From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Pythonic infinite for loop?
Date Fri, 15 Apr 2011 09:13:31 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Lines 16
Message-ID <roy-20FED5.09133115042011@news.panix.com> (permalink)
References <mailman.377.1302833455.9059.python-list@python.org> <4da7ae8a$0$29986$c3e8da3$5496439d@news.astraweb.com> <BANLkTinV2NTUgGNDf+mM1R98vf2TmoQ5Rw@mail.gmail.com> <mailman.382.1302839905.9059.python-list@python.org> <4da83f8f$0$29986$c3e8da3$5496439d@news.astraweb.com>
NNTP-Posting-Host localhost
X-Trace reader1.panix.com 1302873213 21624 127.0.0.1 (15 Apr 2011 13:13:33 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Fri, 15 Apr 2011 13:13:33 +0000 (UTC)
User-Agent MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3270

Show key headers only | View raw


In article <4da83f8f$0$29986$c3e8da3$5496439d@news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:

> for key in dct:
>     if key.startswith("Keyword"):
>         maxkey = max(maxkey, int(key[7:]))

I would make that a little easier to read, and less prone to "Did I 
count correctly?" bugs with something like:

prefix = "Keyword"
n = len(prefix)
for key in dct:
    name, value = key[:n], key[n:]
    if name == prefix:
        maxkey = max(maxkey, int(value))

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


Thread

Pythonic infinite for loop? Chris Angelico <rosuav@gmail.com> - 2011-04-15 12:10 +1000
  Re: Pythonic infinite for loop? Nobody <nobody@nowhere.com> - 2011-04-15 03:33 +0100
  Re: Pythonic infinite for loop? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-15 02:33 +0000
    Re: Pythonic infinite for loop? Chris Angelico <rosuav@gmail.com> - 2011-04-15 13:58 +1000
      Re: Pythonic infinite for loop? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-15 12:52 +0000
        Re: Pythonic infinite for loop? Roy Smith <roy@panix.com> - 2011-04-15 09:13 -0400
        Re: Pythonic infinite for loop? Chris Angelico <rosuav@gmail.com> - 2011-04-16 01:35 +1000
          Re: Pythonic infinite for loop? Paul Rubin <no.email@nospam.invalid> - 2011-04-15 09:10 -0700
  Re: Pythonic infinite for loop? Paul Rubin <no.email@nospam.invalid> - 2011-04-15 00:24 -0700
    Re: Pythonic infinite for loop? Chris Angelico <rosuav@gmail.com> - 2011-04-15 17:35 +1000
      Re: Pythonic infinite for loop? Paul Rubin <no.email@nospam.invalid> - 2011-04-15 00:47 -0700
        Re: Pythonic infinite for loop? Peter Otten <__peter__@web.de> - 2011-04-15 10:11 +0200
  Re: Pythonic infinite for loop? Peter Otten <__peter__@web.de> - 2011-04-15 10:25 +0200
    Re: Pythonic infinite for loop? Chris Angelico <rosuav@gmail.com> - 2011-04-15 18:32 +1000
      Re: Pythonic infinite for loop? Peter Otten <__peter__@web.de> - 2011-04-15 11:32 +0200
      Re: Pythonic infinite for loop? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-15 12:48 +0000

csiph-web