dropwhile question

Rajanikanth Jammalamadaka rajanikanth at gmail.com
Sun Aug 24 08:07:52 CEST 2008


Thanks for the explanations.

Regards,

Raj

On Sat, Aug 23, 2008 at 3:41 PM, Scott David Daniels
<Scott.Daniels at acm.org> wrote:
> Rajanikanth Jammalamadaka wrote:
>>>>>
>>>>> list(itertools.dropwhile(lambda x: x<5,range(10)))
>>
>> [5, 6, 7, 8, 9]
>>
>> Why doesn't this work?
>>>>>
>>>>> list(itertools.dropwhile(lambda x: 2<x<5,range(10)))
>>
>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>
> Because it drops _while_ the condition is True (which it is for
> the first 0 entries in the sequence).  What you want is:
>
>    list(x for x in range(10) if 2 < x < 5)
>
> Note that:
>    list(itertools.dropwhile(lambda x: x<5, range(10)+range(10)))
> is [5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
> not [5, 6, 7, 8, 9, 5, 6, 7, 8, 9].
>
> --Scott David Daniels
> Scott.Daniels.Acm.Org
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
"For him who has conquered the mind, the mind is the best of friends;
but for one who has failed to do so, his very mind will be the
greatest enemy."

Rajanikanth



More information about the Python-list mailing list