我很感激在寻找和理解pythonic方法的一些帮助,以优化嵌套for循环中的以下数组操作:
def _func(a, b, radius):
"Return 0 if a>b, otherwise return 1"
if distance.euclidean(a, b) < radius:
return 1
else:
return 0
def _make_mask(volume, roi, radius):
mask = numpy.zeros(volume.shape)
for x in range(volume.shape[0]):
for y in range(volume.shape[1]):
for z in range(volume.shape[2]):
mask[x, y, z] = _func((x, y, z), roi, radius)
return mask
Run Code Online (Sandbox Code Playgroud)
其中volume.shape(182,218,200)和roi.shape(3,)都是ndarray类型; 而且radius是一个int