1

This is a continuation from here.

If I have an array a and an array b and a can be smaller or larger than b.Is there a way , having as base array the a, to replace all elements of b (element by element) with elements of a?

For example, this works because a has as many elements as b:

import numpy as np

a = np.array([1,2,3,4,5,6,7,8])
b = np.array([[1,2,3,4],[11,22,33,44]])
print(b[0].shape)

i= 0
for el in b:
    for idx,x in enumerate(el):
        el[idx] = a[i]
        i+= 1

print(b)
[[1 2 3 4]
 [5 6 7 8]]

Now, for example, a can be:

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

and b:

b = np.array([11,22,33,44])

The result I want is b = [1,2,3,4,5] because all the 4 elements of b are replaced by the first 4 elements of a and I add one more element to b (the last of a) in order to be the same size as a(my reference array).

If I have :

a = np.array([1,2,3]) and b = np.array([6,7,8,9]) , then the result b array will be : b = [1,2,3] , so I deleted one element from b (to be the same size as a) and replaced the rest with the a elements.

Now, the whole problem is that:

I will have an array which has number of elements multiples of a size.

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

arr = np.array([1,2,3,4,5,6,7,8,9,10])

b = np.array([[0,1,2,3], [4,5,6,7]])

I want to replace the elements from arr to b in multiples of a size (hence 5).

So , final b = [ [1,2,3,4,5], [6,7,8,9,10])

6
  • Can you just write -> b = np.array(a) ? Commented Feb 16, 2017 at 8:00
  • bit confused with definition of arr and a here. Can you tell what is arr in your first example? Is it a=np.array([1]) and arr=np.array([1,2,3,4,5])? Commented Feb 16, 2017 at 8:25
  • @Rohanil:In my first example arr was just the elements of b( I changed the name to el now)if you mean that.Generally, I want to replace all b elements by arr values by multiples of a size (so by every 5 elements in my example) Commented Feb 16, 2017 at 8:32
  • When do you want a to add a row? Only when b.size // a.size > 1, b.size % a.size == 0? Commented Feb 16, 2017 at 9:36
  • And what happens when a.size % b.shape[1] /= 0? Commented Feb 16, 2017 at 9:39

2 Answers 2

3

I think you want

b = arr.reshape(-1, a.size)
b
Out[291]: 
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10]])
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks!It works great!(upvoted because I already accepted)! It gives the same results as the accepted answer (I am answering to your comment below)
Accepted answer has output b=[[1 2 3 4] [5 6 7 8]], which is a different size and value than my answer. Which is correct?
No, it's the same if you run the code.Just the poster have placed wrong output ( I will notify him)
I created another question.
2

Try list slicing assuming b is always either 2d or 1d

import numpy as np

arr = np.array([1,2,3,4,5,6,7,8,9,10])
a = np.array([1,2,3,4,5])
b = np.array([[1,2,3,4],[11,22,33,44]])
print(b[0].shape)

list_arr = arr.tolist()
slice_factor = a.size
j=0

temp = []

for j in range(0,len(list_arr),slice_factor):
    k = min(j + slice_factor,len(list_arr))
    temp.append(list_arr[j:k])

b = np.array(temp)

print(b)
[[1 2 3 4 5]
 [6 7 8 9 10]]

4 Comments

Thanks!Great!And it works for every size, even if b.size/a.size <1 (upvoted)
That . . .doesn't give the output you asked for.
Please correct your output to be right.The result b is [[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]] and not [[1 2 3 4] [5 6 7 8]]
I created another question.

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.