英文:
Replacing values in a string expression based on a matrix and iterating over columns
问题
在列X
中,将有每一列j
中的值的变量,这种情况下只有U1
、X4
和U2
有值,属于列表['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2']
的其余变量都将具有值0
# 示例矩阵
new_matrix = [['C', 'X', 'B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'],
[0.0, 'U1', 8, 2.0, 1.0, -1.0, 0, 0, 1.0, 0],
['+M', 'X4', 2, 1.0, 1.0, 0, 1.0, 0, 0, 0],
['+M', 'U2', 8, 1.0, 2.0, 0, 0, -1.0, 0, 1.0]]
variables = ['X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2', 'B'] # 选择第一行(只有变量)
variables_j_col_values = [['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'], []] # --> ['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2']
问题是我需要创建以下变量的值矩阵(不使用库),其中我会有以下内容:
variables_j_col_values = [['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'],
[0, 0, 0, 0, 2, 0, 8, 8], #列 new_matrix[][2]
[0, 0, 0, 0, 1.0, 0, 2.0, 1.0], #列 new_matrix[][3]
[0, 0, 0, 0, 1.0, 0, 1.0, 2.0], #列 new_matrix[][4]
[0, 0, 0, 0, 0, 0, -1.0, 0], #列 new_matrix[][5]
[0, 0, 0, 0, 1.0, 0, 0, 0], #列 new_matrix[][6]
[0, 0, 0, 0, 0, 0, 0, -1.0], #列 new_matrix[][7]
[0, 0, 0, 0, 0, 0, 1.0, 0], #列 new_matrix[][8]
[0, 0, 0, 0, 0, 0, 0, 1.0], ] #列 new_matrix[][9]
创建了variables_j_col_values
后,继续替换funcion_obj_z
字符串中的行值(除了variables_j_col_values
数组的第0行,因为它是标题)。逻辑是使用循环遍历行,并执行.replace(new_matrix[][n], this_element)
。
funcion_obj_z = 'Z = 3 * X1 + 2 * X2 + 0 * X3 + 0 * X4 + 0 * X5 + M * U1 + M * U2'
这样,如果在每个j
迭代中打印j_func
的值,则会在控制台中获得以下所需的正确输出:
# 对于循环,打印替换字符串中的 j 值
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 2 + 0 * 0 + M * 8 + M * 8' # 迭代 1
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 1.0 + 0 * 0 + M * 2.0 + M * 1.0' # 迭代 2
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 1.0 + 0 * 0 + M * 1.0 + M * 2.0' # 迭代 3
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + M * -1.0 + M * 0' # 迭代 4
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 1.0 + 0 * 0 + M * 0 + M * 0' # 迭代 5
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + M * 0 + M * -1.0' # 迭代 6
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + M * 1.0 + M * 0' # 迭代 7
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + M * 0 + M * 1.0' # 迭代 8
英文:
In column X
are those variables that will have values in each column j, in this case only U1
, X4
and U2
have values, the rest of the variables belonging to the list ['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2']
will all have their values 0
#example matrix
new_matrix = [[ 'C', 'X', 'B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'],
[ 0.0, 'U1', 8, 2.0, 1.0, -1.0, 0, 0, 1.0, 0],
['+M', 'X4', 2, 1.0, 1.0, 0, 1.0, 0, 0, 0],
['+M', 'U2', 8, 1.0, 2.0, 0, 0, -1.0, 0, 1.0]]
variables = ['X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2', 'B'] #select the first row (only variables)
variables_j_col_values = [[variables.pop(variables.index('B'))] + variables, []] # --> ['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2']
The problem with this is that I need create the following matrix of values of the variables (without using libraries) where I would have the following:
variables_j_col_values = [['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'],
[ 0 , 0, 0, 0, 2, 0, 8, 8], #column new_matrix[][2]
[ 0 , 0, 0, 0, 1.0, 0, 2.0, 1.0], #column new_matrix[][3]
[ 0 , 0, 0, 0, 1.0, 0, 1.0, 2.0], #column new_matrix[][4]
[ 0 , 0, 0, 0, 0, 0, -1.0, 0], #column new_matrix[][5]
[ 0 , 0, 0, 0, 1.0, 0, 0, 0], #column new_matrix[][6]
[ 0 , 0, 0, 0, 0, 0, 0, -1.0], #column new_matrix[][7]
[ 0 , 0, 0, 0, 0, 0, 1.0, 0], #column new_matrix[][8]
[ 0 , 0, 0, 0, 0, 0, 0, 1.0], ] #column new_matrix[][9]
After create the variables_j_col_values
, go replacing the values of the rows (except for row 0 of the variables_j_col_values
array because it is a header) in the string inside funcion_obj_z
The logic would be to use a loop that goes through the rows, and does a
.replace(new_matrix[][n], this_element)
funcion_obj_z = 'Z = 3 * X1 + 2 * X2 + 0 * X3 + 0 * X4 + 0 * X5 + M * U1 + M * U2'
In this way, using said string as an expression, it would obtain these prints in the console if it printed the value of j_func
in each j
iteration. These would be the desired correct output:
#for loop, print the j string replacement the values in the string
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 2 + 0 * 0 + M * 8 + M * 8' #iteration 1
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 1.0 + 0 * 0 + M * 2.0 + M * 1.0' #iteration 2
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 1.0 + 0 * 0 + M * 1.0 + M * 2.0' #iteration 3
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + M * -1.0 + M * 0' #iteration 4
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 1.0 + 0 * 0 + M * 0 + M * 0' #iteration 5
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + M * 0 + M * -1.0' #iteration 6
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + M * 1.0 + M * 0' #iteration 7
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + M * 0 + M * 1.0' #iteration 8
答案1
得分: 0
尽管这个解决方案不够美观,但它应该为您提供所需的转换:
new_matrix = [['C', 'X', 'B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'],
[0.0, 'U1', 8, 2.0, 1.0, -1.0, 0, 0, 1.0, 0],
['+M', 'X4', 2, 1.0, 1.0, 0, 1.0, 0, 0, 0],
['+M', 'U2', 8, 1.0, 2.0, 0, 0, -1.0, 0, 1.0]]
variables = ['X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2', 'B']
# 创建一个空矩阵
variables_j_col_values = [[0 for _ in range(len(variables))] for _ in range(len(new_matrix[0])-1)]
# 根据new_matrix的标头替换第一行的变量
variables_j_col_values[0] = sorted(variables, key=lambda x: new_matrix[0].index(x))
# 循环遍历所有值行
for row in new_matrix[1:]:
# 根据variables_j_col_values的第一行获取正确的列
col = variables_j_col_values[0].index(row[1])
# 将值和行进行配对并进行相应的更新
for val, target in zip(row[2:], variables_j_col_values[1:]):
target[col] = val
英文:
Albeit an ugly solution, this should give you the transformation you need:
new_matrix = [[ 'C', 'X', 'B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'],
[ 0.0, 'U1', 8, 2.0, 1.0, -1.0, 0, 0, 1.0, 0],
['+M', 'X4', 2, 1.0, 1.0, 0, 1.0, 0, 0, 0],
['+M', 'U2', 8, 1.0, 2.0, 0, 0, -1.0, 0, 1.0]]
variables = ['X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2', 'B']
# create empty matrix
variables_j_col_values = [[0 for _ in range(len(variables))] for _ in range(len(new_matrix[0])-1)]
# replace first row with sorted variables based on new_matrix headers
variables_j_col_values[0] = sorted(variables, key=lambda x: new_matrix[0].index(x))
# loop over all value rows
for row in new_matrix[1:]
# get correct column in variables_j_col_values based
col = variables_j_col_values[0].index(row[1])
# zip the values and rows and update accordingly
for val, target in zip(row[2:], variables_j_col_values[1:]):
target[col] = val
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论