我有三个列表,我想按照它们在列表中出现的顺序将它们的对象相乘,然后在新列表中返回结果:
a = [1,5,4,3]
b = [20, 44, 40, 100]
c = [222, 432, 670, 190]
新列表应具有以下计算结果:
new_list = [(1*20*222),(5*44*432), (4*40*670), (3*100*190)]
new_list = [x * y * z for x, y, z in zip(a, b, c)]
或者,如果您有三个以上的列表,则特别有用:
import operator
new_list = [reduce(operator.mul, lst, 1) for lst in zip(a, b, c)]
[x * y * z for x, y, z in zip(a, b, c)]
遍历“压缩”列表并乘以它们的组件。
In [1]: a = [1,5,4,3]
In [2]: b = [20, 44, 40, 100]
In [3]: c = [222, 432, 670, 190]
In [5]: [(x*y*z) for x,y,z in zip(a,b,c)]
Out[5]: [4440, 95040, 107200, 57000]