Created
February 10, 2016 09:32
-
-
Save kloetzl/e7a060739b7a9fa0e58a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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