Issue
I have an input array
1 2 2
3 2 1
1 2 2
1 2 3
1 1 3
My output should be
3 2 1
1 2 3
This means all the elements repeated in the rows should be deleted. Is it possible to do that in numpy?
Solution
You can first sort each row and then look at the differences between consecutive elements per row: if there is any 0 in a row, it means that row has duplicates and needs to be dropped:
# sort and take the difference within rows
sorted_arr = np.sort(arr)
diffs = np.diff(sorted_arr)
# form a boolean array: does a row contain any duplicates?
to_drop = (diffs == 0).any(axis=1)
# invert the mask and index into original array
result = arr[~to_drop]
to get
>>> result
array([[3, 2, 1],
[1, 2, 3]])
Answered By - Mustafa Aydın
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.