将列表的第一个元素乘以给定的数字并计算结果列表的累积乘积

Multiply the first element of a list by a given number and calculate a cumulative product of the resulting list

我有以下列表:

l = [1, 2, 3, 4, 5, 6]

我想将第一个元素乘以 9(1*9)=9,然后将所有后续项乘以前一个乘法的结果。请参阅以下输出:

[9, 18, 54, 216, 1080, 6480]

您可以更新列表中的第一项,并使用 itertools.accumulate with operator.mul 对其值进行累积乘积:

from operator import mul
from itertools import accumulate

l = [1, 2, 3, 4, 5, 6]

l[0]*=9
list(accumulate(l, mul))
# [9, 18, 54, 216, 1080, 6480]