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


Groups > comp.lang.python > #3250

Re: Pythonic infinite for loop?

From Peter Otten <__peter__@web.de>
Subject Re: Pythonic infinite for loop?
Date 2011-04-15 10:11 +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>
Newsgroups comp.lang.python
Message-ID <mailman.385.1302855113.9059.python-list@python.org> (permalink)

Show all headers | 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