split a list based on a predicate

beginner zyzhu2000 at gmail.com
Thu Oct 9 02:59:51 EDT 2008


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




More information about the Python-list mailing list