python-3.x 基于矩阵替换字符串表达式中的值并迭代列

n9vozmp4  于 2023-06-07  发布在  Python
关注(0)|答案(1)|浏览(147)

在列X中是那些将在每个列j中具有值的变量,在这种情况下,只有U1X4U2具有值,属于列表['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2']的其余变量都将具有其值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']

这样做的问题是,我需要创建以下变量值的矩阵(不使用库),其中我将有以下内容:

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]

在创建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的值,则它将在控制台中获得这些打印。这些将是所需的正确输出

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

mfuanj7w1#

尽管这是一个丑陋的解决方案,但它应该会给予你所需要的转换:

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

相关问题