0

I have 2 numpy arrays. Eg.

A = [[1,2],[3,4],[5,6]]
B = [[5,6],[1,4],[6,5],[1,2]]

I want to get the matching elements in the 2 arrays. i.e [[1,2],[5,6]]

The arrays that I am using in my code are very large. Is there any fast way of implementing it, without using for loop and comparing each elements?

stellasia
  • 5,372
  • 4
  • 23
  • 43
Leo
  • 31
  • 2

2 Answers2

2

Use a list-comprehension:

A = [[1,2],[3,4],[5,6]]
B = [[5,6],[1,4],[6,5],[1,2]]

print([x for x in A if x in B])
# [[1, 2], [5, 6]]
Austin
  • 25,759
  • 4
  • 25
  • 48
-1
A = [[1,2],[3,4],[5,6]]
B = [[5,6],[1,4],[6,5],[1,2]]

print([x for x in A if x in B])
d_kennetz
  • 5,219
  • 5
  • 21
  • 44
Giridhar
  • 46
  • 6