Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

For the particular example you have you can try

sage: l=range(10)
sage: odd=filter(lambda x: x%2, l)
sage: even=filter(lambda x: x%2==0, l)
sage: odd
[1, 3, 5, 7, 9]
sage: even
[0, 2, 4, 6, 8]

Otherwise you can try implementing a naive version of gather_by using dictionaries as follows

def gather_by( l, f ):
    parts = {}
    for e in l:
        cur_key = f(e)
        if parts.has_key( cur_key ):
            parts[cur_key].append( e )
        else:
            parts[cur_key] = [ e ]
    return parts.values()

Then

sage: gather_by( range(10), lambda x: x%2)
[[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]