map - Python, functional programming, mapping to a higher level -


does know how map in python function higher level in nested list, i.e. equivalent map[f, expr, levelspec] in mathematica.

you can trivially roll own

def map_level(f, item, level):     if level == 0:         return f(item)     else:         return [map_level(f, i, level - 1) in item] 
>>> double = lambda x: x * 2 >>> data = [[1, 2, 3], [4, 5, 6]] >>> map_level(double, data, 0) [[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]] >>> map_level(double, data, 1) [[1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6]] >>> map_level(double, data, 2) [[2, 4, 6], [8, 10, 12]] 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -