name
name

Reputation: 235

traversing a list of list and multiplying each elements

    L = [[0.9, 0.7] ,[0.5, 0.6], [1.1, 1.2]]
    a = 0

    for i in L[len(L)-2]:
       a = L[0][i] * L[1][i]

I wish to multiply this so that it becomes. This wont work for more than 2 values

(a = 0.9 x 0.5 x 1.1 + 0.7 x 0.6 x 1.2 = 0.999). Basically given a list of list each with same size multiply it.

without imports

Upvotes: 2

Views: 104

Answers (8)

Daweo
Daweo

Reputation: 36390

As noted you could use zip to get list of tuples, following way

L = [[0.9, 0.7] ,[0.5, 0.6], [1.1, 1.2]]
L2 = list(zip(*L))
print(L2) #[(0.9, 0.5, 1.1), (0.7, 0.6, 1.2)]

which could be converted into list of lists, following way

L2 = [list(i) for i in L2]
print(L2) #[[0.9, 0.5, 1.1], [0.7, 0.6, 1.2]]

Then if you are using Python2 you could use reduce, which without import is impossible in Python3, so you need to implement reduce-multiplication yourself, I would do it following way:

def mult(x):
    while len(x)>=2: x[:2] = [x[0]*x[1]]
    return x[0]

Note that I assumed your list have no less than 1 element and data in your list is correct (might be multipled) and you would not need list anymore after feeding it into mult, consider following example:

t = [9,5,3]
mt = mult(t)
print(mt) # 135 as you might expect
print(t) # [135] i.e. list with 1 element equal to value returned by mult

If this is not issue in this case, you can do:

a = sum([mult(i) for i in L2])
print(a) # 0.9990000000000001

However if you wish to retain original list and use mult, then you could use copy of list, for example:

k = [3,10,5]
mk = mult(k[:])
print(mk) # 150
print(k) # [3,10,5]

Note that mult as showed can be easily reworked to use any arithmetic operation.

Upvotes: 1

user3053452
user3053452

Reputation: 675

Possible solution with numpy:

import numpy as np

L = [[0.9, 0.7] ,[0.5, 0.6], [1.1, 1.2]]
L = np.array(L)
np.sum([np.prod(L[:,i]) for i in range(L.shape[1])])

Traverse through all columns of the 2d array and calculate prod of each column. Then sum all the results.

Output

0.9990000000000001

Upvotes: 1

sunnky
sunnky

Reputation: 175

use functional programming

>>> from functools import reduce
>>> def multiply(x, y):
...     return x * y
>>> L = [[0.9, 0.7] ,[0.5, 0.6], [1.1, 1.2]]
>>> sum([reduce(multiply, item) for item in zip(*L)])
0.9990000000000001

Upvotes: 1

ElConrado
ElConrado

Reputation: 1638

Naive implementation

L = [[0.9, 0.7] ,[0.5, 0.6], [1.1, 1.2]]
b = []
for i in range(len(L)) :
    a=1 
    for j in range(len(L[0])):
        a*=L[i][j]
    b.append(a)

Upvotes: 1

Adam Smith
Adam Smith

Reputation: 54173

You can get the pairs you want by swapping rows to columns, which is done with the idiom

columns = zip(*rows)

Then just implement a product (analogous to sum)

import itertools
import operator

def product(iterable):
    return itertools.reduce(operator.mul, iterable, 1)
    # or:
    # acc = 1
    # for el in iterable:
    #     acc *= el
    # return acc

And use a list comprehension.

results = sum(product(col) for col in columns)

Upvotes: 1

hiro protagonist
hiro protagonist

Reputation: 46849

this is a version that you can use for lists ans sublists of arbitrary length:

from functools import reduce
from operator import mul

L = [[0.9, 0.7], [0.5, 0.6], [1.1, 1.2]]

res = sum(reduce(mul, p, 1) for p in zip(*L))
# 0.999

as requested this is a version without imports:

s = 0
for items in zip(*L):
    prod = 1
    for factor in items:
        prod *= factor
    s += prod

Explanation:

zip selects the corresponding elements of your sublists:

for p in zip(*L):
    print(p)
# (0.9, 0.5, 1.1)
# (0.7, 0.6, 1.2)

the i use reduce and mul to get the product of the tuples and sum them.

Upvotes: 3

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

the general way:

  • zip terms together
  • apply product of elements. There's no built-in but you can do that with reduce
  • in a list comprehension

like this:

from functools import reduce
import operator

L = [[0.9, 0.7] ,[0.5, 0.6], [1.1, 1.2]]

result = [reduce(operator.mul,terms,1) for terms in zip(*L)]

result:

[0.49500000000000005, 0.504]

for the sum let's use sum:

total = sum(reduce(operator.mul,terms,1) for terms in zip(*L))

roughly gives:

0.999

Upvotes: 2

Salvatore
Salvatore

Reputation: 11934

Something like this:

L = [[0.9, 0.7] ,[0.5, 0.6], [1.1, 1.2]]
a = (1, 1)

for i in L:
   a = (a[0] * i[0], a[1] * i[1])

print(a)

which gives

(0.49500000000000005, 0.504)

Upvotes: 1

Related Questions