1

I want to swap elements between two array starting from a particular array index value keeping other values prior to the array index intact.

import numpy as np
r = np.array([10, 20, 30, 40, 50, 60])
p = np.array([70, 80, 90, 100, 110, 120])

t = []
for i in range(len(r)):
    for j in range(len(p)):
        if i >= 3 and j >= 3:
            t.append(p[j])
            p[j] = r[i]
            for k in t:
                r[i] = k

The above code does the task but the values are in reverse order. The value that I want in array p after swapping is:

[70, 80, 90, 40, 50, 60]

and the value that i want in array r after swapping is:

[10, 20, 30, 100, 110, 120]

But in array p I am getting:

[70, 80, 90, 60, 50, 40]

and in array r I am getting:

[10, 20, 30, 120, 110, 100]

I don't know what is wrong with the code.

1
  • i suspect the reverse has to do with how you accumulate temporary values in t. You need to print values during each iteration to track that down. But as the answers show you don't need to use iteration. Commented Jan 4, 2018 at 17:59

3 Answers 3

3
import numpy as np
r = np.array([10, 20, 30, 40, 50, 60])
p = np.array([70, 80, 90, 100, 110, 120])

for i in range(len(r)):
    if (i>=3):
        p[i],r[i] = r[i],p[i]

Above code will do the work for you. You don't need to run two for loop and t array if I understand your problem right. All you want is to swap at some indexes. You can just swap at those indexes as above no need of a temporary array t.

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

4 Comments

Thanks. The above code works well. But, whats wrong with my code?
1) You don't need two loops. 2) You don't need a temporary array t. You only need current index value i.e t=p[i] 3) What exactly is the last for loop doing? It is same as doing r[i]=t[-1]. 4) Print array t to see what it stores. You will see it stores values in random order (undesired order)
Why not range(3,len(r) and skip the if test?
Yes, that makes more sense. Saves some iterations.
2

You can achieve the same without looping:

r = np.array([10, 20, 30, 40, 50, 60])
p = np.array([70, 80, 90, 100, 110, 120])

i = 3
temp = p[i:].copy()
p[i:] = r[i:]
r[i:] = temp

Now:

>>> p
array([70, 80, 90, 40, 50, 60])
>>> r
array([ 10,  20,  30, 100, 110, 120])

Comments

1

You can copy a slice of one array on to the other:

In [113]: r = np.array([10, 20, 30, 40, 50, 60])
     ...: p = np.array([70, 80, 90, 100, 110, 120])
     ...: 
In [114]: t = p.copy()
In [115]: t[3:]=r[3:]
In [116]: t
Out[116]: array([70, 80, 90, 40, 50, 60])

You could also join slices:

In [117]: np.concatenate((p[:3], r[3:]))
Out[117]: array([70, 80, 90, 40, 50, 60])

Those answers create a new array. I think that's clearer than doing an inplace swap. But here's how I'd do the swap

In [128]: temp = r[3:].copy()
In [129]: r[3:]=p[3:]
In [130]: p[3:]=temp
In [131]: r
Out[131]: array([ 10,  20,  30, 100, 110, 120])
In [132]: p
Out[132]: array([70, 80, 90, 40, 50, 60])

I use copy in temp because otherwise a slice produces a view, which will get modified in the next copy. That issue has come up recently when swapping rows of a 2d array.

With lists the swapping is easier - because r[3:] makes a copy.

In [139]: r=r.tolist()
In [140]: p=p.tolist()
In [141]: temp = r[3:]
In [142]: r[3:], p[3:] = p[3:], r[3:]
In [143]: r
Out[143]: [10, 20, 30, 100, 110, 120]
In [144]: p
Out[144]: [70, 80, 90, 40, 50, 60]

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.