split a list based on a predicate

Rajanikanth Jammalamadaka rajanikanth at gmail.com
Thu Oct 9 13:01:44 EDT 2008


Thanks for all of your replies.

Rajanikanth

On Wed, Oct 8, 2008 at 11:59 PM, beginner <zyzhu2000 at gmail.com> wrote:
> Hi,
>
> On Oct 8, 6:36 pm, "Rajanikanth Jammalamadaka" <rajanika... at gmail.com>
> wrote:
>> Hi!
>>
>> Is there a functional way to do this?
>>
>> I have an array [0,1,2,3,0,1,2,2,3] and I want the first chunk of
>> non-decreasing values from this array (eg: In this case I want
>> [0,1,2,3])
>>
>> Thanks,
>>
>> Rajanikanth
>
> Here is an idea. It is not the most efficient code!
>
> def combine(l, a):
>    if not l or l[-1]<a: l.append(a)
>    return l
>
> reduce(combine, [0,1,2,3,0,1,2,2,3], [])
>
> best regards,
> beginner
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Rajanikanth



More information about the Python-list mailing list