当前位置: 首页 > 面试题库 >

在numpy数组中查找唯一点

雍河
2023-03-14
问题内容

在像这样的numpy数组中查找唯一的x,y点(删除重复项)的更快方法是什么:

points = numpy.random.randint(0, 5, (10,2))

我想到了将点转换为复数,然后检查唯一性的方法,但这似乎有些令人费解:

b = numpy.unique(points[:,0] + 1j * points[:,1])
points = numpy.column_stack((b.real, b.imag))

问题答案:

我会这样:

numpy.array(list(set(tuple(p) for p in points)))

对于大多数情况下的快速解决方案,也许您可​​能会喜欢此食谱:http : //code.activestate.com/recipes/52560-remove-
duplicates-from-a-sequence/



 类似资料: