Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
array manipulation
#1
Hello everyone,

I have question about array manipulation in Numpy. In this case I have 3 arrays:
arr_1 = [ 0 1 2 4 8 3 5 9 6 10 12 7 11 13 14 15]
arr_2 = [1 0 0 1 1 1 0 1]
arr_3 = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

I would like to replace the value of arr_3 with arr_2 based on condition arr_1 (from position of element 9 to 16). For example:
arr_1 element 8 = 6 then arr_3 element 6 = 0 will replaced by arr_2 element 0 = 1 (I am counting the position of element starting from 0 which is the same as program counting the element of array).
arr_1 element 9 = 10 then arr_3 element 10 = 0 will replaced by arr_2 element 1 = 0
arr_1 element 10 = 12 then arr_3 element 12 = 0 will replaced by arr_2 element 2 = 0
arr_1 element 11 = 7 then arr_3 element 7 = 0 will replaced by arr_2 element 3 = 1
arr_1 element 12 = 11 then arr_3 element 11 = 0 will replaced by arr_2 element 4 = 1
arr_1 element 13 = 13 then arr_3 element 13 = 0 will replaced by arr_2 element 5 = 1
arr_1 element 14 = 14 then arr_3 element 14 = 0 will replaced by arr_2 element 6 = 0
arr_1 element 15 = 15 then arr_3 element 15 = 0 will replaced by arr_2 element 7 = 1

After the operation is complete, the value of arr_3 will be:
arr_3 = [0 0 0 0 0 0 1 1 0 0 0 1 0 1 0 1]

The code that I have written so far:
import numpy as np

arr_1 = np.array([0, 1, 2, 4, 8, 3, 5, 9, 6, 10, 12, 7, 11, 13, 14, 15])
arr_2 = np.array([1, 0, 0, 1, 1, 1, 0, 1])
arr_3 = np.zeros((16), dtype=int)
Since I am stuck Wall on this replacement thing, I can't move to the next step.
Reply
#2
This is pretty easy if you ignore the first 8 elements of arr_1. arr_1[8:] are the indices where arr_2 values should be placed in arr_3.
import numpy as np
 
arr_1 = np.array([0, 1, 2, 4, 8, 3, 5, 9, 6, 10, 12, 7, 11, 13, 14, 15])
arr_2 = np.array([1, 0, 0, 1, 1, 1, 0, 1])
arr_3 = np.zeros((16), dtype=int)

for index, value in zip(arr_1[8:], arr_2):
    arr_3[index] = value

print(arr_3)
divon likes this post
Reply
#3
arr1 = [0, 1, 2, 4, 8, 3, 5, 9, 6, 10, 12, 7, 11, 13, 14, 15]
arr2 = [1, 0, 0, 1, 1, 1, 0, 1]
arr3 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
x = len(arr1)-len(arr2)
for n, i in enumerate(arr1[x:]):
    if arr2[n]: arr3[i] = arr2[n]

print(*arr3)
divon likes this post
Reply
#4
Thank you very much for your reply, @deanhystad, and @naughtyCat

Both of your replies are working perfectly, and I can continue with my work.

Once again, Thank you very much.
Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with array manipulation pberrett 4 2,387 Mar-30-2020, 03:02 AM
Last Post: pberrett
  Array manipulation 0zMeister 7 2,793 Feb-15-2020, 12:34 AM
Last Post: 0zMeister
  N-Dim array manipulation in a loop, getting IndexError: too many indices for array cesardepaula 1 4,448 Mar-13-2019, 01:39 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020