Python,累加“ itertools.accumulate”对象不支持项目分配错误

我有以下代码:

diff = [num1-num2 for (num1, num2) in zip(saldo, versamenti)]
iva_a_debito=dict()
iva_a_debito['Iva a debito'] = accumulate(list(diff))

for index, xi in enumerate(iva_a_debito['Iva a debito']):
      if xi < 0 :
          iva_a_debito['Iva a debito'][index] = xi*-1

      else:
           iva_a_debito['Iva a debito'][index] = 0

但是python给我以下错误:

TypeError: 'itertools.accumulate' object does not support item assignment

错误在哪里?

评论
  • bsequi
    bsequi 回复

    将其转换为列表,以便您可以对其进行更新。

    iva_a_debito['Iva a debito'] = list(accumulate(list(diff)))