Reputation: 11
I want to add values from one array to another array after making some calculation, here I did it using for loop but I want efficient way doing so. Please help me.. thanx
from numpy import *
arr = array([1,2,3,4,5])
arr1 = []
for i in arr:
arr1.append(i+5)
print(arr1)
nArray = array(arr1)
print(nArray)
Output :
[6, 7, 8, 9, 10]
[ 6 7 8 9 10]
Upvotes: 1
Views: 49
Reputation: 86
For a builtin Python list, you can use a list comprehension:
arr1 = [x + 5 for x in arr]
Since you are using a numpy array however, you can just add 5 to the array with:
arr1 = arr + 5
Upvotes: 1