【问题标题】:Implicit looping through numpy array to replace values隐式循环遍历 numpy 数组以替换值
【发布时间】: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


【解决方案1】:

这个问题的答案很简单。为了得到我想要的,我需要做的就是:

打印 values_to_use[array_of_positions]

这给了我我需要的东西。

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 2019-02-18
    • 2013-03-25
    • 1970-01-01
    相关资源
    最近更新 更多