TypeError: 'int' object does not support item assignment
AnsweredI try to get the solution class Solution: ObjVal = 0 X = [[[]]] Y = [[[[]]]] C = [[]] Ct = [[]] Vl = [[[]]] Vr = [[[]]] T = [[[]]] t = [[[[]]]] route_Truck = [] route_UAV = [] def getSolution(self, data, model): solution = Solution() solution.ObjVal = model.ObjVal # X_ijk solution.X = [[([0] * data.vehicleNum) for j in range(data.nodeNum)] for i in range(data.nodeNum)] # Y_ijkd solution.Y = [[([0] * data.nodeNum) for j in range(data.nodeNum)] for k in range(data.vehicleNum) for d in range(data.droneNum)] # C_ik, C_id solution.C = [([0] * data.vehicleNum) for i in range(data.nodeNum)] solution.Ct = [([0] * data.droneNum) for i in range(data.nodeNum)] # Vl, Vr solution.Vl = [[([0] * data.droneNum) for k in range(data.vehicleNum)] for i in range(data.nodeNum)] solution.Vr = [[([0] * data.droneNum) for k in range(data.vehicleNum)] for i in range(data.nodeNum)] # T_ijk, t_ijkd solution.T = [[([0] * data.vehicleNum) for j in range(data.nodeNum)] for i in range(data.nodeNum)] solution.t = [[([0] * data.droneNum) for k in range(data.vehicleNum)] for j in range(data.nodeNum) for i in range(data.nodeNum)] for m in model.getVars(): str = re.split(r"_", m.VarName) if (str[0] == "X" and m.x == 1): solution.X[int(str[1])][int(str[2])][int(str[3])] = m.x print(str, end="") print(" = %d" % m.x) elif (str[0] == "Y" and m.x == 1): solution.Y[int(str[1])][int(str[2])][int(str[3])][int(str[4])] = m.x elif (str[0] == "C" and m.x == 1): solution.C[int(str[1])][int(str[2])] = m.x elif (str[0] == "Ct" and m.x == 1): solution.Ct[int(str[1])][int(str[2])] = m.x elif (str[0] == "Vl" and m.x == 1): solution.Vl[int(str[1])][int(str[2])][int(str[3])] = m.x elif (str[0] == "Vr" and m.x == 1): solution.Vr[int(str[1])][int(str[2])][int(str[3])] = m.x elif (str[0] == "T" and m.x > 0): solution.T[int(str[1])][int(str[2])][int(str[3])] = m.x elif (str[0] == "t" and m.x > 0): solution.t[int(str[1])][int(str[2])][int(str[3])][int(str[4])] = m.x elif (str[0] == "z" and m.x > 0): solution.z = m.x
solution.Y reports the error: Traceback (most recent call last): File "E:/PycharmProjects/project/gurobi.py", line 560, in <module> solution = solution.getSolution(data, model) File "E:/PycharmProjects/project/gurobi.py", line 126, in getSolution solution.Y[int(str[1])][int(str[2])][int(str[3])][int(str[4])] = m.x TypeError: 'int' object does not support item assignment
0
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
This error is a Python error and is not related to Gurobi.
The error happens when an integer is made to act as a list. For example, the script below will generate the same error as you observed:A = [1, 2, 3]
# A[2] is an integer and cannot be treated as a list
A[2][0] = 4You might want to check the initialization of the \(\texttt{solution.Y}\) attribute in your code to ensure \(\texttt{solution.Y[i][j][k]}\) is a list and not an integer.
Best regards,
Maliheh0
Post is closed for comments.
Comments
2 comments