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


Groups > comp.lang.python > #3243

Re: Pythonic infinite for loop?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '"if': 0.04; 'string,': 0.05; '(currently': 0.07; 'dictionary': 0.07; 'exception,': 0.07; 'figures': 0.07; 'present,': 0.07; "(it's": 0.09; 'after,': 0.09; 'ah,': 0.09; 'csv': 0.09; 'hits': 0.09; 'loop.': 0.09; 'skip:[ 30': 0.09; 'pm,': 0.11; 'this:': 0.11; 'skip:[ 20': 0.12; 'wrote:': 0.14; '"and': 0.16; 'consecutive': 0.16; 'file).': 0.16; 'keyword,': 0.16; 'op.': 0.16; 'row': 0.16; 'subject:Pythonic': 0.16; 'ignore': 0.16; 'mapping': 0.19; 'object,': 0.19; 'wrap': 0.19; 'seems': 0.21; 'maybe': 0.21; '(or': 0.22; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'values': 0.23; 'keys': 0.23; "didn't": 0.25; "i'm": 0.26; "i'll": 0.26; 'chris': 0.27; 'pass': 0.27; 'function': 0.27; 'message-id:@mail.gmail.com': 0.28; 'thanks': 0.29; 'subject:?': 0.29; 'fri,': 0.29; 'assuming': 0.29; 'odd': 0.29; 'order.': 0.29; "won't": 0.30; 'important,': 0.31; 'does': 0.31; "can't": 0.31; 'match': 0.31; 'to:addr:python-list': 0.32; 'pure': 0.33; 'someone': 0.33; "isn't": 0.34; 'options': 0.34; 'starting': 0.34; 'certain': 0.34; 'difference': 0.35; 'there': 0.35; 'sense,': 0.35; 'think': 0.36; 'received:209.85': 0.37; 'apr': 0.38; 'steven': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'no.': 0.38; 'though': 0.38; 'not,': 0.39; 'larger': 0.39; 'need.': 0.39; 'end': 0.39; 'set': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'received:209': 0.39; 'would': 0.40; "it's": 0.40; 'header:Received:5': 0.40; 'best': 0.60; 'increase': 0.61; 'give': 0.61; 'order': 0.61; '2011': 0.62; 'waste': 0.65; 'care': 0.67; 'postage': 0.84; 'readability': 0.84; 'received:209.85.210.174': 0.84; 'received:mail- iy0-f174.google.com': 0.84; 'top!': 0.84; 'conditions,': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=zr6x8u7YaTMIxBl4xMhzlNzDBH6pPL/DOT8UMTCZaxQ=; b=HoGXYa0hrpMfJpOgq5Jrz1f6vUUBXcsh8dNFcOZy8aLin25v1MKymruG+uH1wE9CW2 n8YM12RiSMN1KT5RmItghpaernhgPjf//ItWPJfJ0nLfvf3IOVuvrrPkATuOb8MmaMq6 FAWnjLM4vWyjzhX+NU65YIX5yFP4dBpDXZ/Xg=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=rO7NMhoX08XhszSs8DfCVjJZa0CAQ9h7N9nWTv6sxF3VCBqJQ+Pgb5uWgWnqibMF7c v2jatoyhAdUDQMxiXmfCzf+a3/N2OPmt3rW5HXmcHrPMi18v6TmVFgM31A+5ZLSkKhJH VpRuPHmccRyut6lFx6CrmaDBEN2JeNR68uXMs=
MIME-Version 1.0
In-Reply-To <BANLkTinV2NTUgGNDf+mM1R98vf2TmoQ5Rw@mail.gmail.com>
References <mailman.377.1302833455.9059.python-list@python.org> <4da7ae8a$0$29986$c3e8da3$5496439d@news.astraweb.com> <BANLkTinV2NTUgGNDf+mM1R98vf2TmoQ5Rw@mail.gmail.com>
Date Fri, 15 Apr 2011 13:58:22 +1000
Subject Re: 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.382.1302839905.9059.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 82.94.164.166
X-Trace 1302839906 news.xs4all.nl 81473 [::ffff:82.94.164.166]:56293
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3243

Show key headers only | View raw


Thanks for the responses, all! In its strictest sense,
itertools.count() seems to be what I'm after, but may not be what I
need.

On Fri, Apr 15, 2011 at 12:33 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> No. But you can use an itertools.count([start=0]) object, and then catch
> the KeyError when you pass the end of the dict. But assuming keys are
> consecutive, better to do this:
>
> lst = [parse_kwdlist(dct["Keyword%d"%i]) for i in xrange(1, len(dct)+1)]

Ah, didn't think to use len(dct) as the top! And yes, the keys will be
consecutive (or rather, if someone omits Keyword5 then I can
justifiably ignore Keyword6).

> If you don't care about the order of the results:
>
> lst = [parse_kwdlist(value) for value in dct.values()]

No, order will matter (I have to pick up the first N that fit certain
conditions, after parsing).

The dictionary is potentially a lot larger than this particular set of
values (it's a mapping of header:value for a row of a user-provided
CSV file). Does this make a difference to the best option? (Currently
I'm looking at "likely" figures of 60+ keys in the dictionary and 3-8
postage options to pick up, but both of those could increase
significantly before production.)

Efficiency is important, though not king; this whole code will be
inside a loop. But readability is important too.

I can't just give all of dct.values() to parse_kwdlist; the function
parses a particular format of text string, and it's entirely possible
that other values would match that format (some of them are pure
free-form text). This has to get only the ones starting with Keyword,
and in order. Steven, the line you suggested:

lst = [parse_kwdlist(dct["Keyword%d"%i]) for i in xrange(1, len(dct)+1)]

will bomb with KeyError when it hits the first one that isn't present,
I assume. Is there an easy way to say "and when you reach any
exception, not just StopIteration, return the list"? (I could wrap the
whole "lst = " in a try/except, but then it won't set lst at all.)

If not, I think I'll go with:

for i in xrange(1,len(dct)+1):

and otherwise as per OP. Having a check for "if key%d in dct" going
all the way up seems like an odd waste of effort (or maybe I'm wrong
there).

Chris Angelico

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