Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » Python

dropwhile question

 
Jump to:  
 
Rajanikanth Jammalamadaka
PostPosted: Sat Aug 23, 2008 7:54 pm    Post subject: dropwhile question
       
Quote:
list(itertools.dropwhile(lambda x: x<5,range(10)))
[5, 6, 7, 8, 9]


Why doesn't this work?
Quote:
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
 

 
Marc 'BlackJack' Rintsch
PostPosted: Sat Aug 23, 2008 8:12 pm    Post subject: Re: dropwhile question
       
On Sat, 23 Aug 2008 14:54:09 -0700, Rajanikanth Jammalamadaka wrote:

Quote:
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
 

 
Fredrik Lundh
PostPosted: Sat Aug 23, 2008 8:14 pm    Post subject: Re: dropwhile question
       
Rajanikanth Jammalamadaka wrote:

Quote:
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 works exactly as specified:

Quote:
help(itertools.dropwhile)
Help on class dropwhile in module itertools:


class dropwhile(__builtin__.object)
| dropwhile(predicate, iterable) --> dropwhile object
|
| Drop items from the iterable while predicate(item) is true.
| Afterwards, return every element until the iterable is exhausted.

Quote:
2<0<5
False


maybe you meant to use itertools.ifilter?

Quote:
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.

Quote:
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]


</F>
 

 
Fredrik Lundh
PostPosted: Sat Aug 23, 2008 8:28 pm    Post subject: Re: dropwhile question
       
Fredrik Lundh wrote:

Quote:
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:

Quote:
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>
 

 
Scott David Daniels
PostPosted: Sat Aug 23, 2008 8:41 pm    Post subject: Re: dropwhile question
       
Rajanikanth Jammalamadaka wrote:
Quote:
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
 

 
Rajanikanth Jammalamadaka
PostPosted: Sun Aug 24, 2008 4:07 am    Post subject: Re: dropwhile question
       
Thanks for the explanations.

Regards,

Raj

On Sat, Aug 23, 2008 at 3:41 PM, Scott David Daniels
<Scott.Daniels@acm.org> wrote:
Quote:
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
--
LINK




--
"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
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

stroje newsy.bober.duu.pl Podczas ÅšwiÄ…t - Lipska Ewa dessous apteka