英文:
How to find 2 integers that can form the numerical values of a list and structure the answers in another list?
问题
#给定以下两个示例表格(总是两个表格具有相同的维度)
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])
#创建结果表格并对收益进行分解的代码
#此处存在问题
#打印获取的结果
print(tabla_resultado)
for lista in tabla_resultado: print(lista)
我将分解tabla_ganancias
中与tabla_asignaciones
中的非空单元格(即非None)对应的值,将收益值的分解放置在最后一行和最后一列中。在这种情况下,收益的值将被分解(分解是指找到两个整数,它们相加得到):
收益值5变成2行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
,但具有收益分解的行和列。
#分解的值将存储在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])
#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)
I decompose the values of tabla_ganancias
that correspond to the non-empty cells (that is, they are not None) of tabla_assignments
, placing the decomposition of the values of the earnings in the last of the rows and in the last of the columns .
In this case, the values of the profits would be broken down (by breaking down I mean finding 2 integers that add up to form):
The gain value 5 become in 2 in a row and 3 in a column ---> 2 + 3 = 5
The gain value 6 become in 2 in a row and 4 in a column ---> 2 + 4 = 6
The gain value 5 become in 2 in a row and 3 in a column ---> 2 + 3 = 5
The gain value 5 become in 2 in a row and 3 in a column ---> 2 + 3 = 5
The gain value 3 become in 1 in a row and 2 in a column ---> 1 + 2 = 3
The gain value 0 become in 2 in a row and -2 in a column ---> 2 + (-2) = 0
The gain value 1 become in -2 in a row and 3 in a column ---> -2 + 3 = 1
So that there is a resulting table having in its last row and its last column the decomposition of the profits. Note that tabla_resultado
is the tabla_asignaciones
but with the row and column of the decompositions of the profits
#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]]
This code not work, but I add this
#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)
But I get that error
Traceback (most recent call last):
tabla_resultado[i][j+rows] = y
IndexError: list assignment index out of range
答案1
得分: 1
答案不是修复你的代码中的IndexError
,而是实现你所期望的输出。当你尝试创建一个由None
组成的列表时,避免使用[None] * n
,可以参考这个答案:List of lists changes reflected across sublists unexpectedly。
以下是代码部分:
rows = len(tabla_ganancias)
columns = len(tabla_ganancias[0])
# 为每一行追加一个None
[tabla_asignaciones[t].append(None) for t in range(rows)]
# 为最后一行追加一个由None组成的列表
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):
# 检查tabla_asignaciones中是否为None
if tabla_asignaciones[i][j] is not None:
value = tabla_ganancias[i][j]
# 如果X和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
# 如果X没有值但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]
# 如果X有值但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, None, 80, 60, 2],
[None, None, 330, None, None, 1],
[None, None, 30, None, 90, -2],
[3, 4, 2, 3, 3, None]]
英文:
The answer is not to fix the IndexError
in your code, but to achieve your desired output. And also when you try to create a list of None
, avoid using [None] * n
, refer to this answer : List of lists changes reflected across sublists unexpectedly.
Here is the code, I have added some comments :
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]
The result is tabla_asignaciones
:
> [[140, 220, None, 80, 60, 2],
>
> [None, None, 330, None, None, 1],
>
> [None, None, 30, None, 90, -2],
>
> [3, 4, 2, 3, 3, None]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论