Feb-27-2020, 04:55 PM
Itertools & operator:
from itertools import starmap from operator import mul a = [4, 0.8, 23] b = [0.3, 2, 3] c = list(starmap(mul, zip(a, b)))List comprehension:
a = [4, 0.8, 23] b = [0.3, 2, 3] c = [x * y for x, y in zip(a, b)]With a for-loop and a list:
a = [4, 0.8, 23] b = [0.3, 2, 3] c = [] for x, y in zip(a, b): result = x * y c.append(result)With a generator
from functools import reduce from operator import mul def mul_seq(*sequences): iterator = zip(*sequences) for multiplicators in iterator: yield reduce(mul, multiplicators) a = [4, 0.8, 23] b = [0.3, 2, 3] c = list(mul_seq(a, b)) # or with more than two d = list(mul_seq(a, b, c))If you have big arrays, you should better use numpy, because this is optimized for matrix operation like broadcasting or matrix multiplication.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!