#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_resultadotabla_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

Become the problem to System of Linear Equations enter image description here

推荐答案

答案不是修复代码中的IndexError,而是实现您想要的输出.另外,当你试图创建一个包含None的列表时,避免使用[None] * n,请参考这个答案:List of lists changes reflected across sublists unexpectedly.

以下是代码,我添加了一些注释:

rows = len(tabla_ganancias)
columns = len(tabla_ganancias[0])
# Append a None for each row 
[tabla_asignaciones[t].append(None) for t in range(rows)]

# Append a list of None as the last row
add_row_list = [None for t in range(columns+1)]
tabla_asignaciones.append(add_row_list)

for i in range(rows):
    for j in range(columns):
        # Check if is None in tabla_asignaciones
        if tabla_asignaciones[i][j] is not None:
            value = tabla_ganancias[i][j]
            
            # If don't have value in X and Y
            if add_row_list[j] is None and tabla_asignaciones[i][columns] is None:
                x = value // 2
                y = value - x
                tabla_asignaciones[rows][j] = y
                tabla_asignaciones[i][columns] = x
            # If don't have value in X but have value in  Y
            elif add_row_list[j] is None and  not tabla_asignaciones[i][columns] is None:
                tabla_asignaciones[rows][j] = value - tabla_asignaciones[i][columns]
            # If have value in X but don't have value in  Y
            elif not add_row_list[j] is None and tabla_asignaciones[i][columns] is None:
                tabla_asignaciones[i][columns] = value - tabla_asignaciones[rows][j]

结果是tabla_asignaciones:

[140,220,无,80,60,2],

[无,无,330,无,无,1],

[NONE,NONE,30,NONE,90,-2],

[3,4,2,3,3,无]

Python相关问答推荐

Django:如何将一个模型的唯一实例创建为另一个模型中的字段

使用Python进行网页抓取,没有页面

自定义新元未更新参数

数字梯度的意外值

如何使用entry.bind(FocusIn,self.Method_calling)用于使用网格/列表创建的收件箱

Python中的负前瞻性regex遇到麻烦

如何计算列表列行之间的公共元素

Python会扔掉未使用的表情吗?

如何在msgraph.GraphServiceClient上进行身份验证?

如何检测背景有噪的图像中的正方形

如何让剧作家等待Python中出现特定cookie(然后返回它)?

如何使用数组的最小条目拆分数组

Python解析整数格式说明符的规则?

我如何根据前一个连续数字改变一串数字?

如何在表中添加重复的列?

Python脚本使用蓝牙运行在Windows 11与raspberry pi4

Django admin Csrf令牌未设置

使用Python从rotowire中抓取MLB每日阵容

从列表中获取n个元素,其中list [i][0]== value''

如何创建引用列表并分配值的Systemrame列