Reputation: 119
I have these type of two matrices:
import itertools
import random
tem_1 = itertools.count(1)
A = [[next(tem_1) for i in range(8)] for j in range(4)]
tem_2 = itertools.count(1)
B = [[next(tem_2) for i in range(10)] for j in range(32)]
I want to multiply each row of B with every corresponding element of of A. I will multiply all the first 10 numbers of matrix B with 1, the next 10 numbers ( row2 in B) with 2 and so on. How do I do this?
Upvotes: 0
Views: 64
Reputation: 2967
Forget about creating A
and do
for idx, i in enumerate(k for k in range(1, 33)):
B[idx] = [i * j for j in B[idx]]
If you need to start from some version of A
with the form in the question, you can flatten it first then use it as the argument to enumerate
above
flat_A = (item for sublist in A for item in sublist)
That creates a generator object that yields every subelement of A
though you could use square brackets to create a list of all the subelements.
Upvotes: 1