dropwhile question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rajanikanth Jammalamadaka

    dropwhile question

    >>list(itertool s.dropwhile(lam bda x: x<5,range(10)) )
    [5, 6, 7, 8, 9]

    Why doesn't this work?
    >>list(itertool s.dropwhile(lam bda 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

    #2
    Re: dropwhile question

    On Sat, 23 Aug 2008 14:54:09 -0700, Rajanikanth Jammalamadaka wrote:
    >>>list(itertoo ls.dropwhile(la mbda x: x<5,range(10)) )
    [5, 6, 7, 8, 9]
    >
    Why doesn't this work?
    >>>list(itertoo ls.dropwhile(la mbda 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

    Comment

    • Scott David Daniels

      #3
      Re: dropwhile question

      Rajanikanth Jammalamadaka wrote:
      >>>list(itertoo ls.dropwhile(la mbda x: x<5,range(10)) )
      [5, 6, 7, 8, 9]
      >
      Why doesn't this work?
      >>>list(itertoo ls.dropwhile(la mbda 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(lambd a 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.A cm.Org

      Comment

      • Rajanikanth Jammalamadaka

        #4
        Re: dropwhile question

        Thanks for the explanations.

        Regards,

        Raj

        On Sat, Aug 23, 2008 at 3:41 PM, Scott David Daniels
        <Scott.Daniels@ acm.orgwrote:
        Rajanikanth Jammalamadaka wrote:
        >>>>>
        >>>>list(iterto ols.dropwhile(l ambda x: x<5,range(10)) )
        >>
        >[5, 6, 7, 8, 9]
        >>
        >Why doesn't this work?
        >>>>>
        >>>>list(iterto ols.dropwhile(l ambda 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(lambd a 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.A cm.Org
        --

        >


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

        Comment

        Working...