Multiplication of lists of lists of different lengths

I have:

a = [[1,2],[3,4],[7,10]]
b = [[8,6],[1,9],[2,1],[8,8]]

I want to multiply (in pairs) each element of a with b

1*8+2*6+1*1+2*9+.....+1*8+2*8+3*8+4*6+......+7*8+10*8

Here is my code:

def f(a, b):
    new = [x for x in a or x in b]
    newer = []
    for tuple1, tuple2 in new:
        newer.append(map(lambda s,t: s*t, new, new))
    return sum(newer)

so my attack plan was to get all the lists in one list and then multiply everything together. I saw that this lambda job is to multiply lists in pairs, but I can't get it to work for a single list.

+6
source share
6 answers

This combination is called the Cartesian product. I would use itertools.productfor this, which can easily handle more than two lists if you want.

First, here's a short demo that shows how to get all the pairs and how to use the tuple assignment to capture the individual elements of the subscription pairs.

from itertools import product

a = [[1,2],[3,4],[7,10]]
b = [[8,6],[1,9],[2,1],[8,8]]

for (u0, u1), (v0, v1) in product(a, b):
    print(u0, u1, v0, v1)

Exit

1 2 8 6
1 2 1 9
1 2 2 1
1 2 8 8
3 4 8 6
3 4 1 9
3 4 2 1
3 4 8 8
7 10 8 6
7 10 1 9
7 10 2 1
7 10 8 8

sum , .

total = sum(u0 * v0 + u1 * v1 for (u0, u1), (v0, v1) in product(a, b))
print(total)

593

, , Prune.

-, .;)

a = [[1,2],[3,4],[7,10]]
b = [[8,6],[1,9],[2,1],[8,8]]

print(sum([u*v for u,v in zip(*[[sum(t) for t in zip(*u)] for u in (a, b)])]))

593

, ,

(1 + 3 + 7) * (8 + 1 + 2 + 8) + (6 + 9 + 1 + 8) * (2 + 4 + 10)

.

# Use zip to transpose each of the a & b lists.
for u in (a, b):
    for t in zip(*u):
        print(t)

(1, 3, 7)
(2, 4, 10)
(8, 1, 2, 8)
(6, 9, 1, 8)

,

# Use zip to transpose each of the a & b lists and compute the partial sums.
partial_sums = []
for u in (a, b):
    c = []
    for t in zip(*u):
        c.append(sum(t))
    partial_sums.append(c)
print(partial_sums)        

[[11, 16], [19, 24]]

, . , zip .

total = 0
for u, v in zip(*partial_sums):
    print(u, v)
    total += u * v
print(total)        

11 19
16 24
593
+9

, : . , -, , .

sum1 = sum(sum(_) for _ in a)
sum2 = sum(sum(_) for _ in b)
return sum1 * sum2
+5

. -, , .

-, a b, a b. 1.

, , 2.

, , Python. , , Python.

+1

numpy :

>>> a = np.array(a)
>>> b = np.array(b)
>>> (a @ b.T).sum()
593
+1

To simplify the task, you can use the function itertools.product.

What he does is that he returns the Cartesian product of the iterations passed to him as arguments. After you have a Cartesian product, you can iterate over them and summarize the coordinate pairs.

After the explanation, this is what you want:

from itertools import product


a = [[1, 2], [3, 4], [7, 10]]
b = [[8, 6], [1, 9], [2, 1], [8, 8]]


def multiply(a, b):
    sum = 0
    for pair1, pair2 in product(a, b):
        sum += pair1[0]*pair2[0] + pair1[1]*pair2[1]
    return sum


print multiply(a, b) #  593
0
source

You can use itertools.productwith zip:

import itertools
a=[[1,2],[3,4],[7,10]]
b=[[8,6],[1,9],[2,1],[8,8]]
result = sum(sum(c*d for c, d in zip(h, k)) for h, k in itertools.product(a, b))
0
source

All Articles