Re: dropwhile question

2008-08-24 Thread Rajanikanth Jammalamadaka
Thanks for the explanations.

Regards,

Raj

On Sat, Aug 23, 2008 at 3:41 PM, Scott David Daniels
[EMAIL PROTECTED] wrote:
 Rajanikanth Jammalamadaka wrote:

 list(itertools.dropwhile(lambda x: x5,range(10)))

 [5, 6, 7, 8, 9]

 Why doesn't this work?

 list(itertools.dropwhile(lambda x: 2x5,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: x5, 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
--
http://mail.python.org/mailman/listinfo/python-list


dropwhile question

2008-08-23 Thread Rajanikanth Jammalamadaka
 list(itertools.dropwhile(lambda x: x5,range(10)))
[5, 6, 7, 8, 9]

Why doesn't this work?
 list(itertools.dropwhile(lambda x: 2x5,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
--
http://mail.python.org/mailman/listinfo/python-list


Re: dropwhile question

2008-08-23 Thread Marc 'BlackJack' Rintsch
On Sat, 23 Aug 2008 14:54:09 -0700, Rajanikanth Jammalamadaka wrote:

 list(itertools.dropwhile(lambda x: x5,range(10)))
 [5, 6, 7, 8, 9]
 
 Why doesn't this work?
 list(itertools.dropwhile(lambda x: 2x5,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
--
http://mail.python.org/mailman/listinfo/python-list


Re: dropwhile question

2008-08-23 Thread Fredrik Lundh

Rajanikanth Jammalamadaka wrote:


list(itertools.dropwhile(lambda x: x5,range(10)))

[5, 6, 7, 8, 9]

Why doesn't this work?



list(itertools.dropwhile(lambda x: 2x5,range(10)))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


it works exactly as specified:

 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.

 205
False

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: x5,range(10)))
[0, 1, 2, 3, 4]
 list(itertools.ifilter(lambda x: 2x5,range(10)))
[3, 4]

/F

--
http://mail.python.org/mailman/listinfo/python-list


Re: dropwhile question

2008-08-23 Thread Fredrik Lundh

Fredrik Lundh wrote:


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: x5,range(10)))
[0, 1, 2, 3, 4]
  list(itertools.ifilter(lambda x: 2x5,range(10)))
[3, 4]


or, more likely, ifilterfalse:

 list(itertools.ifilterfalse(lambda x: x5,range(10)))
[5, 6, 7, 8, 9]
 list(itertools.ifilterfalse(lambda x: 2x5,range(10)))
[0, 1, 2, 5, 6, 7, 8, 9]

/F

--
http://mail.python.org/mailman/listinfo/python-list


Re: dropwhile question

2008-08-23 Thread Scott David Daniels

Rajanikanth Jammalamadaka wrote:

list(itertools.dropwhile(lambda x: x5,range(10)))

[5, 6, 7, 8, 9]

Why doesn't this work?

list(itertools.dropwhile(lambda x: 2x5,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: x5, 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