Re: recursive map on nested list

2006-03-21 Thread johnzenger
Uglier than yours, but down to two lines:

def recur_map(f, data):
return [ not hasattr(x, "__iter__") and f(x) or recur_map(f, x) for x
in data ]

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


Re: recursive map on nested list

2006-03-21 Thread bearophileHUGS
I think for most purposes a program like this is short enough:

def recur_map2(fun, data):
if hasattr(data, "__iter__"):
return [recur_map2(fun, elem) for elem in data]
else:
return fun(data)

data = [set([1, 2]), [3], 4, [5, {6:4}, [7, 8]]]
print recur_map2(lambda x: x*2, data)

Bye,
bearophile

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