Python - Filter numpy array filter element values for all other element at the same index without loop

8zzbczxx  于 2023-10-19  发布在  Python
关注(0)|答案(2)|浏览(125)

假设我有一个numpy数组,如下所示:数组的形状是(5,3),有超过100万行,类型为numpy对象。
样品阵列:

  1. x = np.array([['A',1,10],['B',1,20],['C',2,80],['D',3,40],['E',2,50]])

我希望实现以下目标:
如果列Y的值存在于另一行中,但仅存在于整个数组集中的列Y中,则检查列X的值,并且它们不相等,然后过滤记录。

  1. Col X Y Z
  2. [['A' '1' '10'] ---> filter value '1' from the all values of column Y in entire array
  3. ['B' '1' '20'] ---> filter value '1' from the all values of column Y in entire array
  4. ['C' '2' '80'] ---> filter value '2' from the all values of column Y in entire array
  5. ['D' '3' '40'] ---> filter value '3' from the all values of column Y in entire array
  6. ['E' '2' '50']] ---> filter value '2' from the all values of column Y in entire array

例如:当检查行号1时,列y值为 *'1',对于同一行,列x值为 *'A'。因此,第一个过滤器基于列y值“1”;下面的行满足条件。

[['A' '1' '10']
*['B' '1'* '20']]
然后根据第1行中列x的值应用第二个过滤器,该值不等于所有其他过滤行列x的值。
所以在这种情况下,行2满足这两个条件。
['B' '1'* '20']]
注意:本例显示了两条匹配的记录,但实际上,它可以是一条或多条,并且可以位于任何行位置。
接下来,我想执行的是,对于选定的记录(在本例中为第2行),追加到第1行。
请建议
我试过这段代码,但没有舔:

  1. import numpy as np
  2. x = np.array([['A',1,10],['B',1,20],['C',2,80],['D',3,40],['E',2,50]])
  3. y = x
  4. print(x)
  5. print("Result is:",x[np.where(x[:,1] == y[:,1], np.where(x[:,0] != y[:,0][::-1]),False)])
  6. x= --->print(x)
  7. [['A' '1' '10']
  8. ['B' '1' '20']
  9. ['C' '2' '80']
  10. ['D' '3' '40']
  11. ['E' '2' '50']]

结果为:

qlfbtfca

qlfbtfca1#

你的预期结果真的是这样吗?:

  1. arr = [
  2. ["A", 1, 10], # 0
  3. ["B", 1, 20], # 1
  4. ["C", 2, 80], # 2
  5. ["D", 3, 40], # 3
  6. ["E", 2, 50], # 4
  7. ["F", 1, 30], # 5
  8. ["A", 1, 70], # 6
  9. ]
  10. expected_indexes = [
  11. (1, 5),
  12. (0, 5, 6),
  13. (4,),
  14. (),
  15. (2,),
  16. (0, 1, 6),
  17. (1, 5),
  18. ]
  19. expected = [
  20. (["B", 1, 20], ["F", 1, 30]),
  21. (["A", 1, 10], ["F", 1, 30], ["A", 1, 70]),
  22. (["E", 2, 50]),
  23. (),
  24. (["C", 2, 80]),
  25. (["A", 1, 10], ["B", 1, 20], ["A", 1, 70]),
  26. (["B", 1, 20], ["F", 1, 30]),
  27. ]

如果是这样,您可以执行以下操作:

  1. X, Y = arr[:, :2].T
  2. cond1 = Y[:, None] == Y[None, :]
  3. cond2 = X[:, None] != X[None, :]
  4. mask = cond1 & cond2
  1. >>> cond1
  2. array([[ True, True, False, False, False, True, True],
  3. [ True, True, False, False, False, True, True],
  4. [False, False, True, False, True, False, False],
  5. [False, False, False, True, False, False, False],
  6. [False, False, True, False, True, False, False],
  7. [ True, True, False, False, False, True, True],
  8. [ True, True, False, False, False, True, True]])
  1. >>> cond2
  2. array([[False, True, True, True, True, True, False],
  3. [ True, False, True, True, True, True, True],
  4. [ True, True, False, True, True, True, True],
  5. [ True, True, True, False, True, True, True],
  6. [ True, True, True, True, False, True, True],
  7. [ True, True, True, True, True, False, True],
  8. [False, True, True, True, True, True, False]])
  1. >>> mask
  2. array([[False, True, False, False, False, True, False],
  3. [ True, False, False, False, False, True, True],
  4. [False, False, False, False, True, False, False],
  5. [False, False, False, False, False, False, False],
  6. [False, False, True, False, False, False, False],
  7. [ True, True, False, False, False, False, True],
  8. [False, True, False, False, False, True, False]])

然后使用掩码,每行True值的索引对应于expected_indexes
从这里我看不出你如何在没有for循环的情况下工作,但是繁重的工作已经完成了,你再也没有结构化数组了:

  1. >>> indexes = [tuple(np.where(r)[0]) for r in mask]
  2. >>> assert indexes == expected_indexes
  3. >>>
  4. >>> result = [tuple(arr[list(inds)]) for inds in indexes]
展开查看全部
xwbd5t1u

xwbd5t1u2#

你的答案嵌套了两个np.where,真正起作用的是np.where(x[:,0] != y[:,0][::-1]),它返回x的索引,其中列Y相同,但列X不相等。
你要做的就是把索引发送给x。

  1. Print('The answer is', x[np.where(x[:,0] != y[:,0][::-1])])

相关问题