3

How do I find the difference between two 2D array in python ?

First array and second array

arr1 = [[1,1],[1,2],[1,3],[1,4],[1,5]]
arr2 = [[1,2],[1,3],[1,4]]

The result I want

result = [[1,1],[1,5]]
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
Jeremy
  • 55
  • 6
  • Does this answer your question? [Python Numpy get difference between 2 two-dimensional array](https://stackoverflow.com/questions/66674537/python-numpy-get-difference-between-2-two-dimensional-array) – Danilo Mercado Oudalova Oct 21 '21 at 14:02

1 Answers1

3

You can first convert all element to tuple then use set and difference like below:

>>> set(map(tuple,arr1)).difference(map(tuple,arr2))
{(1, 1), (1, 5)}

>>> list(map(list , set(map(tuple,arr1)).difference(map(tuple,arr2))))
[[1,1],[1,5]]
I'mahdi
  • 23,382
  • 5
  • 22
  • 30