1
from numpy import *
arr1 = array([5,10,15,20,30])
arr2 = array([55,16,1,280,60])
arr3 = ([])
k =0
for num1 in arr1:
    num3 = num1 + arr2[k]
    arr3.append(num3)
    k +=1
print(arr3)

What if arr1 or arr2 has more indexes? How can I fix this?

2
  • What is your expected result? Commented Sep 24, 2019 at 21:45
  • 1
    arr3 = arr1 + arr2 for equal shape numpy arrays. If shapes differ the problem is ill defined. Commented Sep 24, 2019 at 22:51

6 Answers 6

3

It's generally a bad idea to from package import * as you can override other packages in your namespace. Numpy has a built in solution to add two arrays together:

import numpy as np
arr1 = np.array([5,10,15,20,30])
arr2 = np.array([55,16,1,280,60])
arr1+arr2

array([ 60, 26, 16, 300, 90]) # 435 ns ± 5.89 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Though, you need the two arrays to have the same length.

If you want to use some standard python functions (and not a for loop) you can use map and zip to handle to arrays that aren't the same length:

list(map(sum, zip(arr1,arr2)))
[60, 26, 16, 300, 90] # 4.45 µs ± 60.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

If you want to use a for loop you can do this:

new_list = []
for i in range(min(len(arr1), len(arr2))):
    new_list.append(arr1[i]+arr2[i])
new_list

[60, 26, 16, 300, 90] # 2.71 µs ± 307 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) All of these assume you want to stop when you reach the end of the shortest list.

Sign up to request clarification or add additional context in comments.

Comments

0

If the arrays are unequal in length, you first need to align the portions that are of the same length, perform your operation (e.g. addition), and then concatenate the remainder of the longer array (possibly applying another operation, but not in this case).

import numpy as np

arr1 = np.array([5,10,15,20,30])
arr2 = np.array([55,16,1,280,60, 70])  # Longer than arr1.

min_len = min(len(arr1), len(arr2))
longer_array = arr1 if len(arr1) > len(arr2) else arr2
partial_result = arr1[:min_len] + arr2[:min_len]
>>> np.concatenate((partial_result, longer_array[min_len:]))
array([ 60,  26,  16, 300,  90,  70])

Comments

0

Simply copy the largest array into new array , and replace each index of new array with addition of respective indexes of two arrays . So that index out of range and copying array of larger array into new can be avoided.

from numpy import *

arr1 = array([1, 2, 3, 4, 5 ])
arr2 = array([6, 7, 8, 9, 10, 11, 12])
arr3 = ([])

if len(arr1) == len(arr2):  # Finding minimum length array
    minLength = len(arr1)
    pass
elif len(arr1) > len(arr2):
    minLength = len(arr2)
    arr3 = arr1.copy()     # Copy max array into new array
else:
    minLength = len(arr1)
    arr3 = arr2.copy()     # Copy max array into new array

for i in range(minLength):
    arr3[i] = arr1[i] + arr2[i]  # adding array and replacing in new array

print(arr3)

Comments

0
from numpy import *

arr1 = array([2, 6, 8, 9, 1])
arr2 = array([1, 2, 3, 4, 5])
list1 = [ ] 
e = 0
for num1 in arr1:
      list1.append(arr2[e] + num1)
      e+=1

print(array(list1))

1 Comment

How does this approach differ from the existing approaches? Why do you prefer this approach? Please edit your question to include this information to help future readers sort through the thread.
0
#import numpy methods
from numpy import *

#initialize two array
arr1 = array([2, 6, 8, 1, 3])
arr2 = array([1, 6, 2, 1, 5])

#declare an empty array
arr3=([])

#find length of first array
n=len(arr1)

#traverse using for loop
for i in range(0,n):
        #append sum of both array in final array
        arr3.append(arr1[i]+arr2[i])

#print final array
print(arr3)

2 Comments

from numpy import * is considered bad practise. Instead, use import numpy as np, like everyone else.
Welcome to Stack Overflow! Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post.
-1

for this we need numpy package to be imported first,use this only when both the array are of the same length.

from numpy import*

arr1 = array([1,2,3,4,5])
arr2 = array([2,3,4,5,6])
for i in range(len(arr1)):
    arr3 = arr1[i] + arr2[i]
    print(arr3)

1 Comment

This might be considered less pythonic than the answer by @hpaulj

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.