Home > Software design >  list indices must be integers or slices, not decimal.Decimal - Using zip() function to take multiple
list indices must be integers or slices, not decimal.Decimal - Using zip() function to take multiple

Time:06-04

Pardon if my phrasing/code lingo isn't correct. Haven't been coding long. So I am trying to iterate multiple variables simultaneously through a definition.

Sub = "SH"
columnName = ["sh01ia","sh01ib","sh01ic","sh01in","sh02ia","sh02ib","sh02ic","sh02in","sh03ia","sh03ib","sh03ic","sh03in"] #column names in currentminuteload
ncolumnName = ["cir01ia","cir01ib","cir01ic","cir01in","cir02ia","cir02ib","cir02ic","cir02in","cir03ia","cir03ib","cir03ic","cir03in"] #currentPeak column names in sub_historical_peak
tcolumnName = ["cir01iatime","cir01ibtime","cir01ictime","cir01intime","cir02iatime","cir02ibtime","cir02ictime","cir02intime","cir03iatime","cir03ibtime","cir03ictime","cir03intime"] #timestamp column names in 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 (i,j,k,l,m) in zip(currentVal,histVal,columnName,ncolumnName,tcolumnName):
    updateTable(tableName, histTable, currentYear, currentMonth, histYear, histMonth, currentVal[i], histVal[j], columnName[k], ncolumnName[l], tcolumnName[m])

print("Update complete")

currentVal and histVal each return a list of decimals. Note that some of the variables used in updateTable() are being passed through from elsewhere. I've tried to delete out parameters of the for loop to determine which was causing the problem, but no matter which or how many I delete, this error occurs.

TypeError: list indices must be integers or slices, not decimal.Decimal

I'm thinking that I'm using the wrong process to iterate through these variables simultaneously.

CodePudding user response:

currentVal and histVal each return a list of decimals

In other words, currentVal is a list of decimal.Decimal.

Thus, the i in for (i, ...) in zip(currentVal, ...): is an element of currentVal - one of these Decimals (not an index into currentVal!).

You're using this Decimal to index into currentVal in the loop: currentVal[i], but that's an error because "list indices must be integers or slices, not decimal.Decimal".

CodePudding user response:

for (i,j,k,l,m) in zip(currentVal,histVal,columnName,ncolumnName,tcolumnName):
    updateTable(tableName, histTable, currentYear, currentMonth, histYear, histMonth, i, j, k, l, m)

needed to take variable names out of the updateTable(...) portion

  • Related