Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > dropwhile question

Reply
Thread Tools

dropwhile question

 
 
Rajanikanth Jammalamadaka
Guest
Posts: n/a
 
      08-23-2008
>>> 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]

Thanks,

Raj

--
"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
 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      08-23-2008
On Sat, 23 Aug 2008 14:54:09 -0700, 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]


It *does* work. `dropwhile()` drops as long as the callable returns a
true value and then it stops dropping. First value is 0 and
``2 < 0 < 5`` is `False` so nothing is dropped.

What have you expected?

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
 
 
 
Rajanikanth Jammalamadaka
Guest
Posts: n/a
 
      08-24-2008
Thanks for the explanations.

Regards,

Raj

On Sat, Aug 23, 2008 at 3:41 PM, Scott David Daniels
<> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Good use for itertools.dropwhile and itertools.takewhile Nick Mellor Python 35 12-06-2012 09:29 PM
Fate of itertools.dropwhile() and itertools.takewhile() Raymond Hettinger Python 17 02-18-2008 12:39 PM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57