Python lambda with double loops -
is possible rewrite following code lambda expression?
for h in range(height): w in range(width): if maskimg[h][w] > 0: maskimg[h][w] = srcimg[h][w]
this not equivalent expression since not in-place, achieve same end matrix using like:
lambda_function = lambda height, width, src, mask: [[src[h][w] if mask[h][w] > 0 else mask[h][w] w in range(width)] h in range(height)]
but not recommend using it, ever. if you're after speed should use numpy these kinds of things. assuming src
, mask
stored in python-lists, same result much, faster doing:
import numpy np src_np = np.array(src) mask_np = np.array(mask) mask_np[mask_np > 0] = src_np[mask_np > 0]
which on computer ~50 times faster solution.
Comments
Post a Comment