在二维数组中删除的行具有相同的值 [英] Removing rows in a 2D array that have the same value
问题描述
我在寻找一个快速的方法来删除重复值present在二维阵列先到先得。我知道一种方法,如果他们是相同的删除行,但没有如果只有一个值是present。
I'm looking for a quick way to remove duplicate values present in a 2D array on a first come first serve basis. I know a way to remove the rows if they are identical, but not if only one of the values is present.
a = array([[0, 1],
[3, 4],
[3, 5],
[2, 5],
[1, 2]])
由于3是present在[1]和[2]我想删除任何未来价值的发生。同样在2 [3]和[4]所以输出将是:
As 3 is present in a[1] and a[2] I would like to delete any future occurrence of a value. Similarly with 2 in a[3] and a[4] So the output would be:
a = array([[0, 1],
[3, 4],
[2, 5]])
如可以看到的,所用的值5重叠的任何建议是AP preciated
As can be seen, there is overlap with the value 5. Any suggestions are appreciated.
推荐答案
一个纯Python方式使用起来会设定一个列表-COM prehension:
A pure-Python way will be to use set with a list-comprehension:
>>> seen = set()
>>> np.array([x for x in a if seen.isdisjoint(x) and not seen.update(x)])
array([[0, 1],
[3, 4],
[2, 5]])
一班轮只是滥用的事实, set.update
回报无
,所以当的 seen.isdisjoint(X)
是真
我们就可以更新看到
使用不seen.update(X)
。
The one-liner simply abuses the fact that set.update
returns None
, so when seen.isdisjoint(x)
is True
we can update the seen
set using not seen.update(x)
.
我们也可以写上code为:
We can also write the above code as:
seen = set()
out = []
for item in a:
# if none of items in current sub-array are present in seen set
# then add current sub-array to our list. Plus update the seen
# set with the items from current sub-array
if seen.isdisjoint(item):
out.append(item)
seen.update(item)
...
>>> out
[array([0, 1]), array([3, 4]), array([2, 5])]
>>> np.array(out)
array([[0, 1],
[3, 4],
[2, 5]])
这篇关于在二维数组中删除的行具有相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!