【发布时间】:2017-03-29 03:11:30
【问题描述】:
我是 python 新手,正在尝试找到一种方法,根据“array_of_positions”中的值将“array_to_replace”中的值隐式替换为“values_to_use”中的两个值之一:
首先,设置:
values_to_use = np.array([[0.5, 0.3, 0.4], [0.6, 0.7, 0.75]])
array_of_positions = np.array([0, 1, 1, 0, 1, 0, 0, 1, 0, 1])
array_to_replace = np.array([[5, 5, 4], [6, 5, 4], [1, 2, 3], [9, 9, 9], [8, 8, 8], [7, 7, 7], [6, 5, 7], [5, 7, 9], [1, 3, 5], [3, 3, 3]])
然后,做我想做的蛮力方法,即根据“array_of_positions”中的条件值替换“array_to_replace”中的值,如下所示:
for pos in range(0, len(aray_to_replace)):
if (array_of_positions[pos] == 0):
array_to_replace[pos] = values_to_use[0]
else:
array_to_replace[pos] = values_to_use[1]
您对如何隐式执行此操作有什么建议吗?
【问题讨论】:
-
请解释你想完成什么。 “做这个”中的“这个”是什么?
-
谢谢。我修改了我的问题,希望能更清楚。
标签: python loops replace implicit