a = [[1 2 3],
[4 2 1],
[1 3 4]]
the answer should be [6 8 12]
I have tried a lot but I'm not able to solve it please help.
CodePudding user response:
import math
a = [[1, 2, 3], [4, 2, 1], [1, 3, 4]]
print([math.prod(r) for r in a])
CodePudding user response:
You could use numpy.prod
import numpy as np
a = [[1, 2, 3], [4, 2, 1],[1, 3, 4]]
print(np.prod(a, 1))
[6 8 12]
CodePudding user response:
without using math or numpy:
def product(myList) :
result = 1
for i in myList:
result = result * i
return result
a = [[1, 2, 3], [4, 2, 1], [1, 3, 4]]
b = [product(l) for l in a]
print(b) # [6, 8, 12]
CodePudding user response:
a= [1, 2, 3, 4, 2, 1, 1, 3, 4]
print([a[idx]*a[idx-1]*a[idx-2] for idx, _ in enumerate(a) if (idx 1)%3==0 ])