You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python使用zip()迭代报列表索引需为整数而非Decimal错误

问题排查结果

你报错的核心原因是混淆了迭代取出的元素值和列表索引的使用逻辑:

  1. 你用zip()拼接多个列表做循环时,for (i,j,k,l,m) in zip(...)里的i/j/k/l/m本身就是每次迭代从对应列表里直接取出的具体元素,不是数字索引:
    • i是currentVal列表里的单个Decimal类型数值
    • j是histVal列表里的单个Decimal类型数值
    • k/l/m分别是三个列名列表里的单个字符串值
  2. 循环体里你写的currentVal[i]、histVal[j]这类代码,相当于把Decimal类型的数值当下标去访问列表,而Python列表只支持整数/切片作为下标,因此触发你看到的类型错误。
修正方案

直接使用zip()迭代取出的元素作为入参即可,不需要再用这些元素去索引原列表。建议把循环变量名改成有实际语义的名称,避免后续再和索引变量混淆,修正后的代码片段如下:

Sub = "SH"
columnName = ["sh01ia","sh01ib","sh01ic","sh01in","sh02ia","sh02ib","sh02ic","sh02in","sh03ia","sh03ib","sh03ic","sh03in"] #currentminuteload对应列名
ncolumnName = ["cir01ia","cir01ib","cir01ic","cir01in","cir02ia","cir02ib","cir02ic","cir02in","cir03ia","cir03ib","cir03ic","cir03in"] #sub_historical_peak中当前峰值对应列名
tcolumnName = ["cir01iatime","cir01ibtime","cir01ictime","cir01intime","cir02iatime","cir02ibtime","cir02ictime","cir02intime","cir03iatime","cir03ibtime","cir03ictime","cir03intime"] #sub_historical_peak中时间戳对应列名
currentVal = [currentPeaks(i,tableName,currentYear,currentMonth)for i in columnName]
print("Successfully collected current peak values for {}".format(Sub))
histVal = histCheck(ncolumnName, histYear, currentYear, currentMonth, histMonth, Sub)

# 修正后的循环
for curr_val, hist_val, col, ncol, tcol in zip(currentVal, histVal, columnName, ncolumnName, tcolumnName):
    updateTable(tableName, histTable, currentYear, currentMonth, histYear, histMonth, curr_val, hist_val, col, ncol, tcol)

print("Update complete")

补充提示:如果后续写循环时确实需要同时拿到列表的索引和对应元素,用enumerate()包装列表即可,不要把取出的元素值误当索引使用。

内容的提问来源于stack exchange,提问作者BabaZuri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.03 10:18:52