Skip to content

Instantly share code, notes, and snippets.

Created February 10, 2016 09:32
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
mat = [[0,1],[1,0]]
#double each element
mat2 = [[ elem * 2 for elem in row] for row in mat]
double = lambda elem: elem * 2
mat3 = map(lambda row: map(double, row), mat)
print(mat3) # python3 prints <map object 0xaddress>
# 2D map function
def map2d(fn, mat):
return map(lambda row: map(fn, row), mat)
mat4 = map2d(double, mat)
print(mat4) # WTF is this <map object>?
# map2d with explicit cast
def map2d_list(fn, mat):
return list(map(lambda row: list(map(fn, row)), mat))
mat5 = map2d_list(double,mat)
print(mat5) # works as expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment