python 3.x - understanding behavior of mapping to an array -
when map
modify array in place? know preferred way iterate on array list comprehension, i'm preparing algorithm ipyparallel, apparently uses map
function. each row of array set of model inputs, , want use map
, in parallel, run model each row. i'm using python 3.4.5 , numpy 1.11.1. need these versions compatibility other packages.
this simple example creates list , leaves input array intact, expected.
grid = np.arange(25).reshape(5,5) grid array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) def f(g): return g + 1 n = list(map(f, grid)) grid array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]])
but when function modifies slice of input row, array modified in place. can explain behavior?
def f(g): g[:2] = g[:2] + 1 return g n = list(map(f, grid)) grid array([[ 1, 2, 2, 3, 4], [ 6, 7, 7, 8, 9], [11, 12, 12, 13, 14], [16, 17, 17, 18, 19], [21, 22, 22, 23, 24]])
Comments
Post a Comment