How to find 2 integers that can form the numerical values of a list and structure the answers in another list?

huangapple go评论86阅读模式
英文:

How to find 2 integers that can form the numerical values of a list and structure the answers in another list?

问题

  1. #给定以下两个示例表格(总是两个表格具有相同的维度)
  2. tabla_ganancias = [[5, 6, 3, 5, 5],
  3. [2, 5, 3, 1, 0],
  4. [2, 4, 0, 1, 1]]
  5. tabla_asignaciones = [[ 140, 220, None, 80, 60],
  6. [None, None, 330, None, None],
  7. [None, None, 30, None, 90]]
  8. tabla_resultado = []
  9. rows = len(tabla_ganancias)
  10. columns = len(tabla_ganancias[0])
  11. #创建结果表格并对收益进行分解的代码
  12. #此处存在问题
  13. #打印获取的结果
  14. print(tabla_resultado)
  15. 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_resultadotabla_asignaciones,但具有收益分解的行和列。

  1. #分解的值将存储在tabla_resultado的最后一行和最后一列中。
  2. #解决问题所需的方程组
  3. # X1 + Y1 = 5
  4. # X2 + Y1 = 6
  5. # X4 + Y1 = 5
  6. # X5 + Y1 = 5
  7. # X3 + Y2 = 3
  8. # X3 + Y3 = 0
  9. # X5 + Y3 = 1
  10. #tabla_resultado = [[ 140, 220, None, 80, 60, Y1],
  11. # [None, None, 330, None, None, Y2],
  12. # [None, None, 30, None, 90, Y3],
  13. # [ X1, X2, X3, X4, X5, None]]
  14. tabla_resultado = [[ 140, 220, None, 80, 60, 2],
  15. [None, None, 330, None, None, 1],
  16. [None, None, 30, None, 90, -2],
  17. [ 3, 4, 2, 3, 3, None]]
英文:
  1. #Given the following 2 example tables (always both tables have the same dimensions)
  2. tabla_ganancias = [[5, 6, 3, 5, 5],
  3. [2, 5, 3, 1, 0],
  4. [2, 4, 0, 1, 1]]
  5. tabla_asignaciones = [[ 140, 220, None, 80, 60],
  6. [None, None, 330, None, None],
  7. [None, None, 30, None, 90]]
  8. tabla_resultado = []
  9. rows = len(tabla_ganancias)
  10. columns = len(tabla_ganancias[0])
  11. #Code that creates result_table and does the decomposition of the profits
  12. #HERE THE PROBLEM
  13. #Print the results obtained
  14. print(tabla_resultado)
  15. 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

  1. #The decomposed values will be stored in the last row and last column of tabla_resultado.
  2. #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
  3. # X1 + Y1 = 5
  4. # X2 + Y1 = 6
  5. # X4 + Y1 = 5
  6. # X5 + Y1 = 5
  7. # X3 + Y2 = 3
  8. # X3 + Y3 = 0
  9. # X5 + Y3 = 1
  10. #tabla_resultado = [[ 140, 220, None, 80, 60, Y1],
  11. # [None, None, 330, None, None, Y2],
  12. # [None, None, 30, None, 90, Y3],
  13. # [ X1, X2, X3, X4, X5, None]]
  14. tabla_resultado = [[ 140, 220, None, 80, 60, 2],
  15. [None, None, 330, None, None, 1],
  16. [None, None, 30, None, 90, -2],
  17. [ 3, 4, 2, 3, 3, None]]

This code not work, but I add this

  1. #Given the following 2 example tables (always both tables have the same dimensions)
  2. tabla_ganancias = [[5, 6, 3, 5, 5],
  3. [2, 5, 3, 1, 0],
  4. [2, 4, 0, 1, 1]]
  5. tabla_asignaciones = [[ 140, 220, None, 80, 60],
  6. [None, None, 330, None, None],
  7. [None, None, 30, None, 90]]
  8. tabla_resultado = []
  9. rows = len(tabla_ganancias)
  10. columns = len(tabla_ganancias[0])
  11. for i in range(rows):
  12. tabla_resultado.append([])
  13. for j in range(columns):
  14. if tabla_asignaciones[i][j] is not None:
  15. value = tabla_ganancias[i][j]
  16. x = value // 2
  17. y = value - x
  18. tabla_resultado[i].append(x)
  19. if len(tabla_resultado) <= j+rows:
  20. tabla_resultado.append([None] * (rows+1))
  21. tabla_resultado[i][j+rows] = y
  22. else:
  23. tabla_resultado[i].append(None)
  24. # Print the results obtained
  25. print(tabla_resultado)
  26. for lista in tabla_resultado: print(lista)

But I get that error

  1. Traceback (most recent call last):
  2. tabla_resultado[i][j+rows] = y
  3. IndexError: list assignment index out of range

Become the problem to System of Linear Equations
How to find 2 integers that can form the numerical values of a list and structure the answers in another list?

答案1

得分: 1

答案不是修复你的代码中的IndexError,而是实现你所期望的输出。当你尝试创建一个由None组成的列表时,避免使用[None] * n,可以参考这个答案:List of lists changes reflected across sublists unexpectedly

以下是代码部分:

  1. rows = len(tabla_ganancias)
  2. columns = len(tabla_ganancias[0])
  3. # 为每一行追加一个None
  4. [tabla_asignaciones[t].append(None) for t in range(rows)]
  5. # 为最后一行追加一个由None组成的列表
  6. add_row_list = [None for t in range(columns+1)]
  7. tabla_asignaciones.append(add_row_list)
  8. for i in range(rows):
  9. for j in range(columns):
  10. # 检查tabla_asignaciones中是否为None
  11. if tabla_asignaciones[i][j] is not None:
  12. value = tabla_ganancias[i][j]
  13. # 如果X和Y都没有值
  14. if add_row_list[j] is None and tabla_asignaciones[i][columns] is None:
  15. x = value // 2
  16. y = value - x
  17. tabla_asignaciones[rows][j] = y
  18. tabla_asignaciones[i][columns] = x
  19. # 如果X没有值但Y有值
  20. elif add_row_list[j] is None and not tabla_asignaciones[i][columns] is None:
  21. tabla_asignaciones[rows][j] = value - tabla_asignaciones[i][columns]
  22. # 如果X有值但Y没有值
  23. elif not add_row_list[j] is None and tabla_asignaciones[i][columns] is None:
  24. tabla_asignaciones[i][columns] = value - tabla_asignaciones[rows][j]

结果是tabla_asignaciones

  1. [[140, 220, None, 80, 60, 2],
  2. [None, None, 330, None, None, 1],
  3. [None, None, 30, None, 90, -2],
  4. [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 :

  1. rows = len(tabla_ganancias)
  2. columns = len(tabla_ganancias[0])
  3. # Append a None for each row
  4. [tabla_asignaciones[t].append(None) for t in range(rows)]
  5. # Append a list of None as the last row
  6. add_row_list = [None for t in range(columns+1)]
  7. tabla_asignaciones.append(add_row_list)
  8. for i in range(rows):
  9. for j in range(columns):
  10. # Check if is None in tabla_asignaciones
  11. if tabla_asignaciones[i][j] is not None:
  12. value = tabla_ganancias[i][j]
  13. # If don't have value in X and Y
  14. if add_row_list[j] is None and tabla_asignaciones[i][columns] is None:
  15. x = value // 2
  16. y = value - x
  17. tabla_asignaciones[rows][j] = y
  18. tabla_asignaciones[i][columns] = x
  19. # If don't have value in X but have value in Y
  20. elif add_row_list[j] is None and not tabla_asignaciones[i][columns] is None:
  21. tabla_asignaciones[rows][j] = value - tabla_asignaciones[i][columns]
  22. # If have value in X but don't have value in Y
  23. elif not add_row_list[j] is None and tabla_asignaciones[i][columns] is None:
  24. 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]]

huangapple
  • 本文由 发表于 2023年6月22日 13:22:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定