我有3个纽比重排与以下结构。 第一列是某个位置(整数),第二列是分数(浮点)。在
输入:
a = [[1, 5.41],
[2, 5.42],
[3, 12.32],
dtype=[('position', '<i4'), ('score', '<f4')])
]
b = [[3, 8.41],
[6, 7.42],
[4, 6.32],
dtype=[('position', '<i4'), ('score', '<f4')])
]
c = [[3, 7.41],
[7, 6.42],
[1, 5.32],
dtype=[('position', '<i4'), ('score', '<f4')])
]
所有3个数组包含相同数量的元素。
我正在寻找一种有效的方法,将这三个2d数组组合成一个基于position列的数组。在
上述示例的输出arary应如下所示:
输出:
^{pr2}$只有位置为3的行在输出数组中,因为这个位置出现在所有3个输入数组中。在
更新:我的天真方法是以下步骤:
更新2: 每个位置值可以在一个、两个或所有三个输入数组中。在我的输出数组中,我只想包含出现在所有3个输入数组中的位置值的行。在
这里有一种方法,我相信它应该相当快。我想你要做的第一件事就是计算每个位置的出现次数。此函数将处理:
现在使用上面的函数形式,您只需要选择出现3次的位置:
^{pr2}$我们将使用搜索排序,以便对a b和c进行排序:
现在,我们可以通过用户搜索排序来查找每个数组中的位置,从而找到我们的每个uniqpo:
使用字典可能有一个更优雅的解决方案,但我首先想到了这个,所以我将把它留给其他人。在
相关问题 更多 >
编程相关推荐