dropwhile question

Fredrik Lundh fredrik at pythonware.com
Sat Aug 23 18:28:55 EDT 2008


Fredrik Lundh wrote:

> maybe you meant to use itertools.ifilter?
> 
>  >>> help(itertools.ifilter)
> Help on class ifilter in module itertools:
> 
> class ifilter(__builtin__.object)
>  |  ifilter(function or None, sequence) --> ifilter object
>  |
>  |  Return those items of sequence for which function(item) is true.
>  |  If function is None, return the items that are true.
> 
>  >>> list(itertools.ifilter(lambda x: x<5,range(10)))
> [0, 1, 2, 3, 4]
>  >>> list(itertools.ifilter(lambda x: 2<x<5,range(10)))
> [3, 4]

or, more likely, ifilterfalse:

 >>> list(itertools.ifilterfalse(lambda x: x<5,range(10)))
[5, 6, 7, 8, 9]
 >>> list(itertools.ifilterfalse(lambda x: 2<x<5,range(10)))
[0, 1, 2, 5, 6, 7, 8, 9]

</F>




More information about the Python-list mailing list