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


Groups > comp.lang.python > #3250

Re: Pythonic infinite for loop?

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 <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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.05; 'python': 0.07; 'all?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; '>>>': 0.12; 'wrote:': 0.14; 'functools': 0.16; 'hitting': 0.16; 'lambda': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:Pythonic': 0.16; 'writes:': 0.20; 'code': 0.22; 'loop': 0.22; 'right,': 0.22; 'away.': 0.23; 'interpreted': 0.23; 'once.': 0.23; 'skip:( 30': 0.24; 'creating': 0.26; "i'm": 0.26; 'chris': 0.27; 'subject:?': 0.29; "won't": 0.30; 'from:addr:web.de': 0.31; 'paul': 0.32; "skip:' 10": 0.32; 'import': 0.32; 'to:addr:python-list': 0.32; 'bit': 0.33; 'header:X-Complaints-To:1': 0.34; 'occurs': 0.35; 'right?': 0.35; 'some': 0.37; 'should': 0.37; 'faster': 0.38; 'but': 0.38; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'header:Mime- Version:1': 0.39; 'header:Received:5': 0.40; 'might': 0.40; 'best': 0.60; 'dealing': 0.71; 'evaluate': 0.72; 'ugly,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Pythonic infinite for loop?
Date Fri, 15 Apr 2011 10:11:39 +0200
Organization None
References <mailman.377.1302833455.9059.python-list@python.org> <7xsjtkx9hy.fsf@ruckus.brouhaha.com> <mailman.383.1302852922.9059.python-list@python.org> <7xfwpkq7l5.fsf@ruckus.brouhaha.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849b88.dip.t-dialin.net
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.385.1302855113.9059.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 82.94.164.166
X-Trace 1302855113 news.xs4all.nl 65870 [::ffff:82.94.164.166]:36713
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3250

Show key headers only | View raw


Paul Rubin wrote:

> Chris Angelico <rosuav@gmail.com> writes:
>>> sentinel = object()
>>> seq = (dct.get('Keyword%d'%i,sentinel) for i in count(1))
>>> lst = list(takewhile(lambda x: x != sentinel, seq))
>>
>> If I understand this code correctly, that's creating generators,
>> right? It won't evaluate past the sentinel at all?
> 
> Right, it should stop after hitting the sentinel once.
> 
>> That might well be what I'm looking for. A bit ugly, but efficient and
>> compact. And I can bury some of the ugliness away.
> 
> It occurs to me, operator.ne might be a little faster than the
> interpreted lambda.

Or operator.is_not as you are dealing with a singleton. You also need 
functools.partial:

$ python -m timeit -s'sentinel = object(); predicate = lambda x: x != 
sentinel' 'predicate(None)'
1000000 loops, best of 3: 0.369 usec per loop

$ python -m timeit -s'sentinel = object(); predicate = lambda x: x is not 
sentinel' 'predicate(None)'
1000000 loops, best of 3: 0.314 usec per loop

$ python -m timeit -s'from functools import partial; from operator import 
ne; sentinel = object(); predicate = partial(ne, sentinel)' 
'predicate(None)'
1000000 loops, best of 3: 0.298 usec per loop

$ python -m timeit -s'from functools import partial; from operator import 
is_not; sentinel = object(); predicate = partial(is_not, sentinel)' 
'predicate(None)'
1000000 loops, best of 3: 0.252 usec per loop


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