#Given the following 2 example tables (always both tables have the same dimensions)
tabla_ganancias = [[5, 6, 3, 5, 5],
[2, 5, 3, 1, 0],
[2, 4, 0, 1, 1]]
tabla_asignaciones = [[ 140, 220, None, 80, 60],
[None, None, 330, None, None],
[None, None, 30, None, 90]]
tabla_resultado = []
rows = len(tabla_ganancias)
columns = len(tabla_ganancias[0])
#Code that creates result_table and does the decomposition of the profits
#HERE THE PROBLEM
#Print the results obtained
print(tabla_resultado)
for lista in tabla_resultado: print(lista)
我分解了与tabla_assignments
的非空单元格对应的值tabla_ganancias
(即,它们不是无),将收入值的分解放在最后一行和最后一列.
在这种情况下,利润的价值将被分解(通过分解,我的意思是找到两个加起来构成的整数):
增益值5在行中变为2,在列中变为3->2+3=5
增益值6在一行中变为2,在一列中变为4-->2+4=6
增益值5在行中变为2,在列中变为3->2+3=5
增益值5在行中变为2,在列中变为3->2+3=5
增益值3在一行中变为1,在列中变为2->1+2=3
增益值0在行中变为2,在列中变为-2-->2+(-2)=0
增益值1在行中变为-2,在列中变为3-2+3=1
因此,结果表的最后一行和最后一列中包含利润分解.请注意,tabla_resultado
是tabla_asignaciones
,但带有利润分解的行和列
#The decomposed values will be stored in the last row and last column of tabla_resultado.
#System of equations that should be solved to find the values that I need to build the last of the rows and the last of the columns of tabla_resultado
# X1 + Y1 = 5
# X2 + Y1 = 6
# X4 + Y1 = 5
# X5 + Y1 = 5
# X3 + Y2 = 3
# X3 + Y3 = 0
# X5 + Y3 = 1
#tabla_resultado = [[ 140, 220, None, 80, 60, Y1],
# [None, None, 330, None, None, Y2],
# [None, None, 30, None, 90, Y3],
# [ X1, X2, X3, X4, X5, None]]
tabla_resultado = [[ 140, 220, None, 80, 60, 2],
[None, None, 330, None, None, 1],
[None, None, 30, None, 90, -2],
[ 3, 4, 2, 3, 3, None]]
这段代码不起作用,但我添加以下内容
#Given the following 2 example tables (always both tables have the same dimensions)
tabla_ganancias = [[5, 6, 3, 5, 5],
[2, 5, 3, 1, 0],
[2, 4, 0, 1, 1]]
tabla_asignaciones = [[ 140, 220, None, 80, 60],
[None, None, 330, None, None],
[None, None, 30, None, 90]]
tabla_resultado = []
rows = len(tabla_ganancias)
columns = len(tabla_ganancias[0])
for i in range(rows):
tabla_resultado.append([])
for j in range(columns):
if tabla_asignaciones[i][j] is not None:
value = tabla_ganancias[i][j]
x = value // 2
y = value - x
tabla_resultado[i].append(x)
if len(tabla_resultado) <= j+rows:
tabla_resultado.append([None] * (rows+1))
tabla_resultado[i][j+rows] = y
else:
tabla_resultado[i].append(None)
# Print the results obtained
print(tabla_resultado)
for lista in tabla_resultado: print(lista)
但我明白这个错误
Traceback (most recent call last):
tabla_resultado[i][j+rows] = y
IndexError: list assignment index out of range