【问题标题】:Given a child (subset of parent) numpy array, find indices of that on parent array?给定一个子(父子集)numpy数组,在父数组上找到它的索引?
【发布时间】:2021-05-10 16:15:43
【问题描述】:

我有两个数组

Parent array (A) = [2,1,2,1,4,4,3]

child array (B) = [1,2,3,4] ( it is actually uniq of A) 



我想使用 numpy 在 A 中为 B 的元素查找索引

在这种情况下,它是

[1,3,0,2,6,4,5]

如果我有这样的字典就好了

{1: [1,3], 2: [0,2], 3:[6], 4: [4,5] }

【问题讨论】:

  • 你探索unique参数了吗?

标签: numpy indices numpy-slicing


【解决方案1】:

for 循环在这里看起来不错:

out_dict = {}
for a in enumerate(A):
    if a in out_dict:
        out_dict.append(i)
    else:
        out_dict[a] = [i]

【讨论】:

  • 我更喜欢使用 NumPy。 A 通常很大
  • 如果您想要字典输出,@ProtonBoss numpy 并没有真正的帮助。
  • @ProtonBoss 在另一方面,你的第一个请求是np.argsort(A)
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 2021-04-09
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-27
  • 1970-01-01
相关资源
最近更新 更多