2

I have a numpy array with another arrays inside and I want to know how I can check if all the values of another numpy array (Or list) are the same of the first one.

array1 = np.array([[11,3,4,6,7,8,9,1,2], [6,7,2,1,9,5,3,4,8]])
array2 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

I tried to do it with the array2 in np.sort(array1) but it return True.

I want to obtain False in the first array of the array [11,3,4,6,7,8,9,1,2] because the number 5 is not in and True in the second one [1, 2, 3, 4, 5, 6, 7, 8, 9]. Thanks for taking your time to read it and trying to help.

Amartin
  • 337
  • 2
  • 17

1 Answers1

3

you are currently checking to see if any of the arrays match.

if you want False and True, you need an elementwise comparison. done via a list comprehension:

[all(array2 == arr) for arr in np.sort(array1)]

which gives [False, True]

the all() is there because just checking array2 == arr will give a list of True/False for each entry, but we want a full match

Zulfiqaar
  • 603
  • 1
  • 6
  • 12