在Python(Numpy、Pytorch)中,在数组中查找列表的元素。

huangapple go评论92阅读模式
英文:

Finding elements of a list in an array in Python (Numpy, Pytorch)

问题

我正在处理以下问题:我有一个列表,假设 a=[1, 2, 3] 和一个数组 b=[[2, 4, 6],[3, 2, 5],[4, 1, 3]],列表中的元素数量等于数组中的行数,输出应该是一个列表 c=[-1, 1, 2],其中 -1 表示数字 a[0] 没有在数组 b 的第0行中找到,其他数字告诉了元素 a[i] 在行 b[i, :] 中的索引,索引从0开始。

我使用循环解决了这个问题,但我正在寻找一种更优化的方法,可以使用 np.where 或 torch 的替代方法来解决,我会感谢任何帮助。

英文:

I am dealing with the following problem: I have a list, let's say a=[1, 2, 3] and an array b=[[2, 4, 6],[3, 2, 5],[4, 1, 3]], the number of elements in the list is equal to the number of rows in the array, the output should be a list c=[-1, 1, 2], where -1 indicates that the number a[0] was not found in the 0-th row of the array b, the other numbers tells the index where the element a[i] is in the row b[i, :], the index enumeration starts at 0.

I solved it using loops but I am searching for a more optimal way to do it using np.where or the torch alternative, I would appreciate any help.

答案1

得分: 2

  1. import numpy as np
  2. a = [1, 2, 3]
  3. b = np.array([[2, 4, 6], [3, 2, 5], [4, 1, 3]])
  4. # 通过将每个元素与a的对应元素进行比较来创建布尔掩码
  5. mask = b == np.array(a)[:, np.newaxis]
  6. # 找到掩码每行中第一个True的索引
  7. indices = np.argmax(mask, axis=1)
  8. # 使用np.where条件地分配匹配发生的索引,未匹配的地方分配-1
  9. c = np.where(np.any(mask, axis=1), indices, -1).tolist()
  10. print(c)
英文:

Try this

  1. import numpy as np
  2. a = [1, 2, 3]
  3. b = np.array([[2, 4, 6], [3, 2, 5], [4, 1, 3]])
  4. # Create a boolean mask by comparing each element of b with the corresponding element of a
  5. mask = b == np.array(a)[:, np.newaxis]
  6. # Find the index of the first occurrence of True along each row of the mask
  7. indices = np.argmax(mask, axis=1)
  8. # Use np.where to conditionally assign the indices where matches were found and -1 where no matches occurred
  9. c = np.where(np.any(mask, axis=1), indices, -1).tolist()
  10. print(c)

Or

  1. import numpy as np
  2. a = [1, 2, 3]
  3. b = np.array([[2, 4, 6], [3, 2, 5], [4, 1, 3]])
  4. c = []
  5. for i in range(len(a)):
  6. # Find the indices where the i-th element of a matches the elements in the i-th row of b
  7. indices = np.where(b[i, :] == a[i])[0]
  8. if len(indices) > 0:
  9. # If indices are found, append the first index to the list c
  10. c.append(indices[0])
  11. else:
  12. # If no indices are found, append -1 to the list c
  13. c.append(-1)
  14. print(c)

Output

  1. [-1, 1, 2]

答案2

得分: 0

假设每行最多只有一个匹配项,你可以使用以下代码:

  1. out = np.full_like(a, -1)
  2. idx, vals = np.where(b == a[:,None])
  3. out[idx] = vals

输出结果:array([-1, 1, 2])

可重现的输入数据:

  1. a = np.array([1, 2, 3])
  2. b = np.array([[2, 4, 6],[3, 2, 5],[4, 1, 3]])
英文:

Assuming at most one match per row, you could use:

  1. out = np.full_like(a, -1)
  2. idx, vals = np.where(b == a[:,None])
  3. out[idx] = vals

Output: array([-1, 1, 2])

Reproducible input:

  1. a = np.array([1, 2, 3])
  2. b = np.array([[2, 4, 6],[3, 2, 5],[4, 1, 3]])

huangapple
  • 本文由 发表于 2023年6月26日 16:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定