python - 查找一个数组中的相同值以及另一个数组中的等价对

标签 python arrays numpy

我有两个这样的数组 a = 点1[:2]b = 点2[:2] 它们的长度相等。现在我想找到 a 中的重复值及其在 b 中各自的索引。一旦我有了这些指数,我想计算均方根。

我尝试过以下方法:

b = point2[:2] 
idx = np.argsort(b)
sorted_z_predval = b[idx]
vals, idx_start, count = 
    np.unique(sorted_z_predval, return_counts=True, return_index=True)
filt = np.split(idx, idx_start[1:])
vals = vals[count >1]
b_uniq = filter(lambda x: x.size>1, filt)`

现在我得到了值重复多次的索引。我需要在 a = point1[:2] 中找到等效的对索引 我对这一点感到震惊。

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

numpy_indexed包(免责声明:我是它的作者)包含将以高效和矢量化的方式执行这些类型的操作的功能:

import numpy_indexed as npi
idx = npi.indices(b, npi.unique(a))

请注意,您可以使用例如 npi.indices(b, npi.unique(a), Missing=-1) 来获取 idx 中的 -1 值,其中没有对应的值b 中的条目。

关于python - 查找一个数组中的相同值以及另一个数组中的等价对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57540676/

相关文章:

python - 获取错误将错误的 SQL 语句打印到标准输出 python

java - 使用字符串数组来标记每个条目的字母

python - 使用 numpy/scipy 的快速 b 样条算法

python - 在条件下向前填充列

python - 为什么 mpirun 在循环中卡住

python - 在 Python 中查询的语法是什么?

python - csvkit & django a.k.a. 使用 csvkit 作为模块而不是从命令行

Javascript将字符串数组的数组转换为数字数组的数组

ruby - 将哈希数组的数组转换为哈希数组

python - Python中根据特定列分解数据集