0

I have a numpy array

a = ([[1,2,3],
      [2,2,2],
      [1,5,3],
      [3,3,1]])


swap1 = [2,2,2]

swap2 = [3,3,1]

I want to swap the rows which are equal to swap1 and swap2 without being aware of the index of these 2 rows. I want the output to look like this

Out = ([[1,2,3],
        [3,3,1],
        [1,5,3],
        [2,2,2]])

What would be the best way to do this? I want to avoid loops if that's an option. Thanks!

5 Answers 5

1
>>> a = np.array([[1,2,3],
      [2,2,2],
      [1,5,3],
      [3,3,1]])
>>> x = [2,2,2]
>>> y = [3,3,1]

Make a boolean array of the rows you are interested in

>>> xmask = np.all(a==x,axis=1)
>>> ymask = np.all(a==y,axis=1)
>>> xmask
array([False,  True, False, False])
>>> ymask
array([False, False, False,  True])

Then use them to change the values

>>> a[xmask] = y
>>> a[ymask] = x
>>> a
array([[1, 2, 3],
       [3, 3, 1],
       [1, 5, 3],
       [2, 2, 2]])
>>>

If the array is square

>>> a = np.array([[1,2,3,4],
      [2,2,2,9],
      [1,5,3,1],
      [3,3,1,8]])
>>> y = [3,3,1,8]
>>> x = [2,2,2,9]
>>> xmask = np.all(a==x,axis=1)
>>> ymask = np.all(a==y,axis=1)
>>> a[xmask,:] = y
>>> a[ymask,:] = x

Boolean indexing

Sign up to request clarification or add additional context in comments.

Comments

0

My solution running on whole list, if that's what you need so all right.

def list_swap(a, swap1, swap2):
# Running on the list
for list in a:
    if (list == swap1):
        list = swap2
    else if (list == swap2):
        list = swap1

In python you can compare lists, so there is a simple function thats running on the list and changing values if she needs to.

Comments

0

use map its very easy.

a = map(lambda x:([3,3,1]) if x == [2,2,2] else ([2,2,2] if x == [3,3,1] else x), a)

or (according to your variables)

a = map(lambda x:(swap2) if x == swap1 else (swap1 if x == swap2 else x), a)

see no loops, a single liner and you got your result

Result.

[[1, 2, 3], [3, 3, 1], [1, 5, 3], [2, 2, 2]]

Comments

0

For numpy array:

import numpy as np

a = np.array([[1,2,3],
              [2,2,2],
              [1,5,3],
              [3,3,1]])

swap1 = [2,2,2]
swap2 = [3,3,1]

id1 = np.where(np.all(a == swap1, axis = 1))
id2 = np.where(np.all(a == swap2, axis = 1))

a[id2], a[id1] = a[id1], a[id2]

print(a)
# [[1 2 3]
#  [3 3 1]
#  [1 5 3]
#  [2 2 2]]


For list:

idxs = [ [idx, e] for idx, e in enumerate(a) if e == swap1 or e == swap2 ]
idxs[0][0], idxs[1][0] = idxs[1][0], idxs[0][0]

for i in idxs:
  a[i[0]] = i[1]
print(a)

#=> [[1, 2, 3], [3, 3, 1], [1, 5, 3], [2, 2, 2]]

1 Comment

It doesn't work for numpy array. The question reads "I have a numpy array"
0
import numpy as np
ar=np.array([[1,2,3],[2,2,2],[1,5,3],[3,3,1]])
swap1 = [2,2,2]

swap2 = [3,3,1]
def swapfunc( x ):
    if (x==swap2).all():
        return swap1
    else:
        return x

ar=np.apply_along_axis( swapfunc, axis=1, arr=ar )
print(ar)

Output

[[1 2 3]
 [2 2 2]
 [1 5 3]
 [2 2 2]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.