Wave1988
Wave1988

Reputation: 57

Compare two arrays in Numpy

After comparison between an array total= np.full((3,3),[0, 1, 2]) and array a = np.array([1],[0],[1]), I am looking to get a new_array:

array([[0, 2],
       [1, 2],
       [0, 2]])

Upvotes: 0

Views: 149

Answers (2)

Lante Dellarovere
Lante Dellarovere

Reputation: 1858

Can apply a mask and reshape:

import numpy as np

total= np.full((3,3),[0, 1, 2])
a = np.array([[1],[0],[1]])

a = np.repeat(a, 3, axis=1)
new_total = total[total!=a].reshape(3, 2)

>>> print(new_total)
[[0 2]
 [1 2]
 [0 2]]

Upvotes: 0

sentence
sentence

Reputation: 8903

You can use list comprehension and np.delete:

import numpy as np

total= np.full((3,3),[0, 1, 2])
a = np.array([[1],[0],[1]])

new_array = np.array([np.delete(l, i) for l,i in zip(total,a)])

result

array([[0, 2],
       [1, 2],
       [0, 2]])

Upvotes: 1

Related Questions