0

I have an array [ 0 10 15 20 10 0 35 25 15 35 0 30 20 25 30 0] and I need to insert each element of another array ' [5,7,8,15] ' at locations with an increment of 5 such that the final array looks [ 0 10 15 20 5 10 0 35 25 7 15 35 0 30 8 20 25 30 0 15] length is 20

I am trying with this code

arr_fla = [ 0 10 15 20 10  0 35 25 15 35  0 30 20 25 30  0]
arr_split = [5,7,8,15]
node = 5   
    node_len = node * (node-1)
    
    for w in range(node, node_len, 5):
        for v in arr_split:
            arr_fla = np.insert(arr_fla,w,v)
    print(arr_fla)

The result I am getting is

'[ 0 10 15 20 10 15  8  7  5  0 15  8  7  5 35 15  8  7  5 25 15 35  0 30
 20 25 30  0]' length 28

Can someone please tell me where I am going wrong.

3
  • 1
    when you insert an element into an array, the elements will shift to the right. Based on your final array, it looks like you want to add only one element from arr_split into arr_fla. See if your insert statement is doing that Commented Aug 6, 2020 at 11:36
  • @JoeFerndz In 'arr_fla' I have 16 elements. I want to insert each element of 'arr_split' at positions of 5 , 10 , 15 and 20, so that the array is now of 20 elements. Each element from 'arr_split' exactly once at the respective place. Commented Aug 6, 2020 at 11:43
  • @JoeFerndz And it is adding all the elements at that position, and I need only one. Commented Aug 6, 2020 at 11:50

5 Answers 5

1

If the sizes line up as cleanly as in your example you can use reshape ...

np.reshape(arr_fla,(len(arr_split),-1))
# array([[ 0, 10, 15, 20],
#        [10,  0, 35, 25],
#        [15, 35,  0, 30],
#        [20, 25, 30,  0]])

... append arr_split as a new column ...

np.c_[np.reshape(arr_fla,(len(arr_split),-1)),arr_split]
# array([[ 0, 10, 15, 20,  5],
#        [10,  0, 35, 25,  7],
#        [15, 35,  0, 30,  8],
#        [20, 25, 30,  0, 15]])

... and flatten again ...

np.c_[np.reshape(arr_fla,(len(arr_split),-1)),arr_split].ravel()
# array([ 0, 10, 15, 20,  5, 10,  0, 35, 25,  7, 15, 35,  0, 30,  8, 20, 25,
#        30,  0, 15]) 
Sign up to request clarification or add additional context in comments.

2 Comments

that also seems to give a slight performance plus compared to np.insert
@MrFuppes if you want it fast you should avoid np.c_ which is for convenience, not speed. And use np.concatenate instead. Will have to manually reshape arr_split, though.
0

I have corrected it:

arr_fla = [0,10,15,20,10,0,35,25,15,35,0,30,20,25,30,0]
arr_split = [5,7,8,15]
node = 5
    
for w in range(len(arr_split)):
    arr_fla = np.insert(arr_fla, (w+1)*node-1, arr_split[w])
print(arr_fla)

'''
Output:
[ 0 10 15 20  5 10  0 35 25  7 15 35  0 30  8 20 25 30  0 15]
'''

In your code:

for v in arr_split:

This gets all the elements at once (in total w times), but you need just one element at a time. Thus you do not need an extra for loop.

Comments

0

You want to have a counter that keeps going up every time you insert the item from your second array arr_split.

Try this code. My assumption is that your last element can be inserted directly as the original array has only 16 elements.

arr_fla = [0,10,15,20,10,0,35,25,15,35,0,30,20,25,30,0]
arr_split = [5,7,8,15]

j = 0 #use this as a counter to insert from arr_split

#start iterating from 4th position as you want to insert in the 5th position

for i in range(4,len(arr_fla),5): 

    arr_fla.insert(i,arr_split[j]) #insert at the 5th position every time
    #every time you insert an element, the array size increase

    j +=1 #increase the counter by 1 so you can insert the next element

arr_fla.append(arr_split[j]) #add the final element to the original array 
print(arr_fla)

Output:

[0, 10, 15, 20, 5, 10, 0, 35, 25, 7, 15, 35, 0, 30, 8, 20, 25, 30, 0, 15]

Comments

0

You could split the list in even chunks, append to each the split values to each chunk, and reassemble the whole (credit to Ned Batchelder for the chunk function ):

arr_fla = [0,10,15,20,10,0,35,25,15,35,0,30,20,25,30,0]
arr_split = [5,7,8,15]
node = 5  

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

tmp_arr = chunks(arr_fla, node)

arr_out = []

for index, chunk in enumerate(tmp_arr):
    if arr_split[index]: # make sure arr_split is not exhausted
        chunk.append(arr_split[index]) # we use the index of the chunks list to access the split number to insert 
    arr_out += chunk

print(arr_out)

Outputs:

[0, 10, 15, 20, 10, 5, 0, 35, 25, 15, 35, 7, 0, 30, 20, 25, 30, 8, 0, 15]

Comments

0

you can change to below and have a try.

import numpy as np

arr_fla = [0, 10, 15, 20, 10, 0, 35, 25, 15, 35, 0, 30, 20, 25, 30, 0]
arr_split = [5, 7, 8, 15]

index = 4
for ele in arr_split:
    arr_fla = np.insert(arr_fla, index, ele)
    index += 5
print(arr_fla)

the result is

[ 0 10 15 20  5 10  0 35 25  7 15 35  0 30  8 20 25 30  0 15]

about the wrong part of yours, I think it's have two questions:

  1. the second loop is no need, it will cause np insert all the element of arr_split at the same position
  2. the position is not start at 5, it should be 4

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.