dropwhile question

Discussion in 'Python' started by Rajanikanth Jammalamadaka, Aug 23, 2008.

  1. >>> 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
     
    Rajanikanth Jammalamadaka, Aug 23, 2008
    #1
    1. Advertising

  2. 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
     
    Marc 'BlackJack' Rintsch, Aug 23, 2008
    #2
    1. Advertising

  3. 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
     
    Rajanikanth Jammalamadaka, Aug 24, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    629
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    700
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    980
    Scott Allen
    Oct 6, 2005
  4. Raymond Hettinger
    Replies:
    17
    Views:
    578
    Simon Brunning
    Feb 18, 2008
  5. Nick Mellor
    Replies:
    35
    Views:
    398
    Paul Rubin
    Dec 6, 2012
Loading...

Share This Page