Manseka
Manseka

Reputation: 33

How to Sum Two List N-Element

I need to add two lists of numbers. When one list is greater than the other I get the error "IndexError: list index out of range". The error occurs in line resultado = v1[i] + v2[i], but I don't know how to solve it.

My code is:

def suma(A, B):

    if len(A) > len(B):
        v1 = A
        v2 = B
    else:
        v1 = B
        v2 = A
    v3 = []
    i = 0
    for i in range(len(v1)):
        resultado = v1[i] + v2[i]
        v3.append(resultado)
    for j in range(len(v1), len(v2)):
        v3.append(v2[j])
    return v3

vectorA = []
vectorB = []
x = 1
while x !=0:
    print "Ingrese los datos para el primer vector (0 para finalizar)"
    x = input("-->>")
    if x !=0:
        vectorA.append (x)

print "=" * 30
x = 1
while x !=0:
    print "Ingrese los datos para el segundo vector (0 para finalizar)"
    x = input("-->>")
    if x !=0:
        vectorB.append (x)

print "=" * 30
print vectorA
print vectorB
print "=" * 30
print "A) SUMA DE VECTORES"
print "=" * 30

suma(vectorA, vectorB)
print suma

Upvotes: 3

Views: 526

Answers (3)

likeitlikeit
likeitlikeit

Reputation: 5638

What's going to help you more with learning Python at this stage is probably this:

The actual error is caused because you use the length of the longer list to iterate over both lists. It is easily resolved by changing

for i in range(len(v1)):

into

for i in range(len(v2)):

because, v2 is the shorter list after the comparison above. You should also replace

for j in range(len(v1), len(v2)):        
    v3.append(v2[j])

by

for j in range(len(v2), len(v1)):        
    v3.append(v1[j])

because, after all, len(v2) is smaller than len(v1). Finally replace

suma(vectorA, vectorB)
print suma

by

print suma(vectorA, vectorB)

to have the result printed. You find the working script below.

def suma(A, B):

    if len(A) > len(B):
        v1 = A
        v2 = B
    else:
        v1 = B
        v2 = A
    v3 = []
    i = 0
    for i in range(len(v2)):
        resultado = v1[i] + v2[i]
        v3.append(resultado)
    for j in range(len(v2), len(v1)):
        v3.append(v1[j])
    return v3

vectorA = []
vectorB = []
x = 1
while x !=0:
    print "Ingrese los datos para el primer vector (0 para finalizar)"
    x = input("-->>")
    if x !=0:
        vectorA.append (x)

print "=" * 30
x = 1
while x !=0:
    print "Ingrese los datos para el segundo vector (0 para finalizar)"
    x = input("-->>")
    if x !=0:
        vectorB.append (x)

print "=" * 30
print vectorA
print vectorB
print "=" * 30
print "A) SUMA DE VECTORES"
print "=" * 30

print suma(vectorA, vectorB)

Upvotes: 2

inspectorG4dget
inspectorG4dget

Reputation: 113945

You want itertools.izip_longest

def suma(A, B):
    return map(sum, itertools.izip_longest(A,B, fillvalue=0))

Upvotes: 2

Pavel Anossov
Pavel Anossov

Reputation: 62908

>>> import itertools
>>> A = [1, 2, 3]
>>> B = [10, 11]
>>> [a + b for a, b in itertools.izip_longest(A, B, fillvalue=0)]
[11, 13, 3]

So,

def suma(A, B):
    return [a + b for a, b in itertools.izip_longest(A, B, fillvalue=0)]

Upvotes: 7

Related Questions