Question: Replacing values in a string expression based on a matrix and iterating over columns

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



Answer:
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