Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'processing.': 0.03; 'ryan': 0.05; 'dictionary': 0.07; 'infinite': 0.07; 'numeric': 0.07; 'x-mailer:evolution 2.28.3': 0.07; 'python': 0.07; 'content- type:multipart/signed': 0.09; 'hurt': 0.09; 'iterate': 0.09; 'kelly': 0.09; 'output': 0.12; 'skip:[ 20': 0.12; 'def': 0.13; 'wrote:': 0.14; 'appends': 0.16; 'begging': 0.16; 'content- type:application/pgp-signature': 0.16; 'filename:fname piece:asc': 0.16; 'filename:fname piece:signature': 0.16; 'filename:fname:signature.asc': 0.16; 'keyerror:': 0.16; 'pythonic': 0.16; 'subject:Pythonic': 0.16; 'top,': 0.16; 'xrange': 0.16; 'things.': 0.16; 'input': 0.18; 'yield': 0.19; 'wondering': 0.19; 'to:2**1': 0.20; 'cheers,': 0.20; 'header:In- Reply-To:1': 0.22; 'loop': 0.22; 'skip:k 20': 0.23; 'skip:l 30': 0.25; "i'm": 0.26; 'chris': 0.27; 'object': 0.27; 'function': 0.27; 'looks': 0.28; 'string': 0.29; 'subject:?': 0.29; 'fri,': 0.29; 'elements': 0.29; 'probably': 0.30; '(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; 'received:192': 0.34; 'received:192.168.0': 0.35; 'there': 0.35; 'question': 0.35; 'put': 0.35; 'apologies': 0.35; 'try:': 0.35; 'doing': 0.36; 'received:au': 0.36; 'think': 0.36; 'received:192.168': 0.37; 'two': 0.37; 'some': 0.37; 'but': 0.38; 'draft': 0.39; 'url:au': 0.39; 'to:addr:python.org': 0.39; 'comes': 0.39; 'header:Mime-Version:1': 0.39; 'how': 0.39; 'except': 0.39; 'takes': 0.40; 'would': 0.40; 'might': 0.40; 'details': 0.64; 'visit': 0.67; 'become': 0.70; 'vital': 0.77; '12:10': 0.84; 'readability': 0.84; 'received:192.168.0.5': 0.84; 'ask.': 0.91 Subject: Re: Pythonic infinite for loop? From: Ryan Kelly To: Chris Angelico , python-list@python.org In-Reply-To: References: Content-Type: multipart/signed; micalg="pgp-sha1"; protocol="application/pgp-signature"; boundary="=-Ij7GtrZ6iGOnODaYAlVo" Date: Fri, 15 Apr 2011 12:34:17 +1000 Mime-Version: 1.0 X-Mailer: Evolution 2.28.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 94 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1302834871 news.xs4all.nl 81483 [::ffff:82.94.164.166]:45346 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3237 --=-Ij7GtrZ6iGOnODaYAlVo Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, 2011-04-15 at 12:10 +1000, Chris Angelico wrote: > Apologies for interrupting the vital off-topic discussion, but I have > a real Python question to ask. >=20 > 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. >=20 > My first draft looks something like this. The input dictionary is > called dct, the output list is lst. >=20 > lst=3D[] > for i in xrange(1,10000000): # arbitrary top, don't like this > try: > lst.append(parse_kwdlist(dct["Keyword%d"%i])) > except KeyError: > break >=20 > 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.) There is, use an infinite generator: def ints_from(start): while True: yield start start +=3D 1 for i in ints_from(1): ..etc... But why not just put the while loop inline: i =3D 0 while True: try: ...etc... except KeyError: break i +=3D 1 It might be even easier to just iterate through the dictionary keys: for k in sorted(dct.keys()): if k.startswith("Keyword"): lst.append(parse_kwdlist(dct[k])) > 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. You probably could, but I think it would hurt readability in this case: lst =3D [parse_kwdlist(dct[k]) for k in sorted(dct.keys()) if k.startswith("Keyword")] Cheers, Ryan --=20 Ryan Kelly http://www.rfk.id.au | This message is digitally signed. Please visit ryan@rfk.id.au | http://www.rfk.id.au/ramblings/gpg/ for details --=-Ij7GtrZ6iGOnODaYAlVo Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iEYEABECAAYFAk2nrqIACgkQfI5S64uP50pUJwCgs33kmZJI1WJKnG7NjKAGj2Bp b1IAoIPFYbmwvTTq78TQVGslChx8Uozw =Icgk -----END PGP SIGNATURE----- --=-Ij7GtrZ6iGOnODaYAlVo--