Skip to content

Instantly share code, notes, and snippets.

@kloetzl
Created February 10, 2016 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kloetzl/e7a060739b7a9fa0e58a to your computer and use it in GitHub Desktop.
Save kloetzl/e7a060739b7a9fa0e58a to your computer and use it in GitHub Desktop.
#!/usr/bin/python
mat = [[0,1],[1,0]]
print(mat)
#double each element
mat2 = [[ elem * 2 for elem in row] for row in mat]
print(mat2)
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