w3resource

NumPy: Take values from a source array and put them at specified indices of another array

NumPy: Array Object Exercise-100 with Solution

Write a NumPy program to take values from a source array and put them at specified indices of another array.

Sample Solution:

Python Code:

import numpy as np
x = np.array([10, 10, 20, 30, 30], float)
print(x)
print("Put 0 and 40 in first and fifth position of the above array")
y = np.array([0, 40, 60], float) 
x.put([0, 4], y)
print("Array x, after putting two values:")
print(x)

Sample Output:

[ 10.  10.  20.  30.  30.]                                                             
Put 0 and 40 in first and fifth position of the above array                            
Array x after put two values:                                                          
[  0.  10.  20.  30.  40.]

Pictorial Presentation:

Python NumPy: Take values from a source array and put them at specified indices of another array

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to sum and compute the product of a numpy array elements.
Next: Write a NumPy program to print the full NumPy array, without truncation.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}