0

Given a numpy array with multiple arrays inside, how do I replace all the values of the array with values from another array?

For example:

import numpy
first_array = numpy.array([[1,2],[3,4],[5,6],[7,8],[9,10]])

second_array = numpy.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
 0.7, 0.8, 0.9, 1])

Given these arrays, How do I replace 1,2 with 0.1, 0.2 and etc?

4
  • first_array = second_array.reshape((5,2)) is the easy way. As long as the total number of elements is the same, just reshape it. Commented Jul 29, 2021 at 20:50
  • What is the rule that tells you how to do the replacement? What is the exact desired result in this case, and how is it different from just using second_array directly? Commented Jul 29, 2021 at 20:50
  • @TimRoberts Probably better to use first_array.shape than hard-code (5,2). [EDIT: I now see that an answer has been posted, which does exactly this.] Commented Jul 29, 2021 at 20:58
  • first_array dtype is int, so you can't put floats in it. In any case reshaping the 2nd array is more efficient. Commented Jul 29, 2021 at 22:31

2 Answers 2

1

Use np.reshape

# import numpy as np

>>> m
array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10]])

>>> n
array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])

>>> n.reshape(m.shape)
array([[0.1, 0.2],
       [0.3, 0.4],
       [0.5, 0.6],
       [0.7, 0.8],
       [0.9, 1. ]])
Sign up to request clarification or add additional context in comments.

Comments

0
first_array = np.array([[1,2],[3,4],[5,6],[7,8],[9,10]])
second_array = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6,0.7, 0.8, 0.9, 1])

np.set_printoptions(formatter={'float':"{0:0.1f}".format})
first_array = first_array.astype(float)

for i in range(np.shape(first_array)[0]):
    for j in range(np.shape(first_array)[1]):
        first_array[i][j] = second_array[2*i+j]

print(first_array)

Output:

[[0.1 0.2]
 [0.3 0.4]
 [0.5 0.6]
 [0.7 0.8]
 [0.9 1.0]]

3 Comments

Of course, replacing a whole array with another array is silly, but what if I purposely create a np.zeros array, and then replacing it element by element with element from another calculation, I mean instead of append just do element replacement. (maybe the asker just write the question as a whole array with another array replacement, but actually what he wanted is just to know generally how to do element by element replacement array[i][j] =)
There's a debate to be had. If you're working with Python lists, then it's almost always way better to create a new list from scratch rather than replacing each element one by one. With numpy arrays, it's not always that straightforward, but the reshape solution is obviously much better than element-wise replacement.
You are right, it's almost always way better, until the homework explicitly state that we need to do element by element replacement (real example in my case: making hysteresis loop with 1000 MonteCarlo steps, where the initial array keep changing over time)

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.