Skip to content Skip to sidebar Skip to footer

Removing Rows In A 2d Array That Have The Same Value

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 on

Solution 1:

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) andnot seen.update(x)])
array([[0, 1],
       [3, 4],
       [2, 5]])

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).

We can also write the above code as:

seen =set()
out= []
for item in a:
    # if noneof items incurrent sub-arrayare present in seen set
    # thenaddcurrent sub-arrayto our list. Plus update the seen
    # setwith the items fromcurrent 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]])

Post a Comment for "Removing Rows In A 2d Array That Have The Same Value"