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


Groups > comp.lang.python > #3231

Pythonic infinite for loop?

Return-Path <rosuav@gmail.com>
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; 'processing.': 0.03; 'dictionary': 0.07; 'numeric': 0.07; 'python': 0.07; 'output': 0.12; 'advance': 0.14; '"hey': 0.16; 'adam': 0.16; 'appends': 0.16; 'begging': 0.16; 'keyerror:': 0.16; 'pythonic': 0.16; 'subject:Pythonic': 0.16; 'top,': 0.16; 'xrange': 0.16; 'things.': 0.16; 'input': 0.18; 'wondering': 0.19; 'loop': 0.22; 'received:209.85.214.174': 0.23; 'received:mail- iw0-f174.google.com': 0.23; "i'm": 0.26; 'chris': 0.27; 'object': 0.27; 'function': 0.27; 'message-id:@mail.gmail.com': 0.28; 'looks': 0.28; 'thanks': 0.29; 'string': 0.29; 'subject:?': 0.29; 'elements': 0.29; 'forgot': 0.29; '(the': 0.30; 'list': 0.30; 'this.': 0.30; 'one,': 0.31; 'turned': 0.31; 'construct': 0.31; 'value)': 0.31; "can't": 0.31; 'called': 0.32; 'to:addr:python- list': 0.32; 'beginning': 0.33; 'break': 0.33; 'generally': 0.33; 'there': 0.35; 'question': 0.35; 'apologies': 0.35; 'try:': 0.35; 'doing': 0.36; 'two': 0.37; 'some': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'but': 0.38; 'draft': 0.39; 'received:209.85.214': 0.39; 'to:addr:python.org': 0.39; 'comes': 0.39; 'received:209': 0.39; 'how': 0.39; 'except': 0.39; 'takes': 0.40; 'header:Received:5': 0.40; 'become': 0.70; 'vital': 0.77; 'ask.': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=Dr29gP1gx87uQfNxGzS2es1CHSSWRedAkQZlmOIHZNQ=; b=eUBKBQR9NSqfYNsiQSwwMs8ScrHQotUTpLpVBAGRmVmTXY8Si5oheHMsQghwinkiIK m3SMKOcVKaJKqI2DM+2Q3/37OZxkHsReIlSwSRkkhgo1YzA45pwpF8ghhB1wLl/4zTAm vpC80qlPH5dWHiEWzXn4vjNvVdVGzpWZEvkQw=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=RNOc0PzIpxv5gREJ/sd+c7lIqmDEgAW6Fhw5+Fqo++k42PPV61G3yzSmFlWkGTisWn rPt6ZDXMnIDhjf/KxNkSRzjRQbz65U+B7BIsSLYn7nmjcBS4YEqEKe42FHJ+gRakSAcD RPj7Am8kKn+x2mWCYSJ5W4hXIZJeZUzaw75Kc=
MIME-Version 1.0
Date Fri, 15 Apr 2011 12:10:52 +1000
Subject Pythonic infinite for loop?
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.377.1302833455.9059.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 82.94.164.166
X-Trace 1302833455 news.xs4all.nl 32470 [::ffff:82.94.164.166]:47621
X-Complaints-To abuse@xs4all.nl
Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3231

Show key headers only | View raw


Apologies for interrupting the vital off-topic discussion, but I have
a real Python question to ask.

I'm doing something that needs to scan a dictionary for elements that
have a particular beginning and a numeric tail, and turn them into a
single list with some processing. I have a function parse_kwdlist()
which takes a string (the dictionary's value) and returns the content
I want out of it, so I'm wondering what the most efficient and
Pythonic way to do this is.

My first draft looks something like this. The input dictionary is
called dct, the output list is lst.

lst=[]
for i in xrange(1,10000000): # arbitrary top, don't like this
  try:
    lst.append(parse_kwdlist(dct["Keyword%d"%i]))
  except KeyError:
    break

I'm wondering two things. One, is there a way to make an xrange object
and leave the top off? (Sounds like I'm risking the numbers
evaporating or something.) And two, can the entire thing be turned
into a list comprehension or something? Generally any construct with a
for loop that appends to a list is begging to become a list comp, but
I can't see how to do that when the input comes from a dictionary.

In the words of Adam Savage: "Am I about to feel really, really stupid?"

Thanks in advance for help... even if it is just "hey you idiot, you
forgot about X"!

Chris Angelico

Back to comp.lang.python | Previous | NextNext 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