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, d

recursive map on nested list

2006-03-21 Thread alexandre_irrthum
Hello, I'd like to apply a function to elements of a nested list and wondered if there is anything more idiomatic and/or shorter than this recursive way: >>> def recur_map(f, data): ... if isinstance(data, list): ... mapped_list = [] ... for i in data: ...