【问题标题】:Construct a ndarray from ndarray according to list of coordinate [duplicate]根据坐标列表从数组构造一个数组[重复]
【发布时间】:2020-08-15 12:56:58
【问题描述】:

我想根据一些坐标从srcndarray 构建一个ndarray taget。这是一个例子

src = np.arange(12).reshape(3,4)
coordinates = [[[0,0],[0,1],[0,3]],
               [[2,1],[1,1],[0,1]]]
target = src.SOME_API(coordinates)
# expect target as
# [[0,1,3],
#  [9,5,1]]

我该怎么做?

【问题讨论】:

    标签: python numpy numpy-ndarray numpy-slicing


    【解决方案1】:

    您可以使用此元组索引来获取每组索引的值,然后将其转置以获得所需的形状:

    target = src[tuple(coordinates.T)].T
    

    输出:

    [[0 1 3]
     [9 5 1]]
    

    【讨论】:

      猜你喜欢
      • 2019-05-03
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      • 2020-09-08
      相关资源
      最近更新 更多