How to sort array elements based on the closest occurrence of a sublist with a numeric value of 1.0? And then combine this sorted matrix with another
You need to sort the titles together with the other lists. You can do it with zip
matrix = [['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'], [8, 2.0, 1.0, -1.0, 0, 0, 1.0, 0], [2, 1.0, 1.0, 0, 1.0, 0, 0, 0], [8, 1.0, 2.0, 0, 0, -1.0, 0, 1.0]]
matrix_aux = [['X4', 'U1', 'U2'], [0, 1.0, 0], [1.0, 0, 0], [0, 0, 1.0]]

matrix_aux_ord = list(zip(*sorted(zip(*matrix_aux), key=lambda x: x[1:].index(1.0))))
print(matrix_aux_ord)

# [('U1', 'X4', 'U2'), (1.0, 0, 0), (0, 1.0, 0), (0, 0, 1.0)]

And to add the titles to matrix use list comprehensions
titles = ['X'] + list(matrix_aux_ord[0])
new_matrix = [[titles[i]] + matrix[i] for i in range(len(titles))]
print(new_matrix)

# [['X', 'B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'], ['U1', 8, 2.0, 1.0, -1.0, 0, 0, 1.0, 0], ['X4', 2, 1.0, 1.0, 0, 1.0, 0, 0, 0], ['U2', 8, 1.0, 2.0, 0, 0, -1.0, 0, 1.0]]


How do I correct my code to be able to order its elements according to which has the canonical vector with a value equal to 1.0 in the element closest to the beginning of its sublists (ignoring the first sublist, which is the one with the titles, although this will also change position according to the position of element 1.0 in the other remaining ones), thus remaining?
matrix = [['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'], [8, 2.0, 1.0, -1.0, 0, 0, 1.0, 0], [2, 1.0, 1.0, 0, 1.0, 0, 0, 0], [8, 1.0, 2.0, 0, 0, -1.0, 0, 1.0]]

matrix_aux = [['X4', 'U1', 'U2'], [0, 1.0, 0], [1.0, 0, 0], [0, 0, 1.0]]

#Extract the first title sublist
titles = matrix_aux.pop(0)

#Create a list of tuples, and sort it
tuple_list = [(sublist.index(1.0), sublist) for sublist in matrix_aux]
sorted_tuples = sorted(tuple_list, key=lambda x: x[0])

#Rebuild the sorted array
matrix_aux_ord = [[titles[i] for i in range(len(titles))]] + [sublist for _, sublist in sorted_tuples]

print(matrix_aux_ord)
for row in matrix_aux_ord: print(row) #print in matrix format

the problem now with my code is that it forgets to sort the row of titles or headers ['X4', 'U1', 'U2'], incorrectly printing this matrix
[['X4', 'U1', 'U2'], [1.0, 0, 0], [0, 1.0, 0], [0, 0, 1.0]]

instead of this that if it maintains consistency
[['U1', 'X4', 'U2'], [1.0, 0, 0], [0, 1.0, 0], [0, 0, 1.0]]

Then, having these 2 matrices, build the new matrix called new_matrix, in which I would add a column in front of the matrix, that is, I would add an element to each of the sublists that make up the rows of matrix, to the first sublist of the matrix called matrix add an 'X' before it as the first element, and to the rest of the sublists of the matrix called matrix add as the first element in an ordered manner the elements of the first sublist of the matrix called matrix_aux_ord
matrix = [['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'], [8, 2.0, 1.0, -1.0, 0, 0, 1.0, 0], [2, 1.0, 1.0, 0, 1.0, 0, 0, 0], [8, 1.0, 2.0, 0, 0, -1.0, 0, 1.0]]

#If the previous code worked, it would get this array sorted like this...
matrix_aux_ord = [['U1', 'X4', 'U2'], [1.0, 0, 0], [0, 1.0, 0], [0, 0, 1.0]]

#Add the column title or header 'X' to the front of the first sublist of matrix
matrix[0].insert(0, 'X')

So the resulting final matrix, the correct output, called as new_matrix, would look like this:
new_matrix = [['X', 'B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'], ['U1', 8, 2.0, 1.0, -1.0, 0, 0, 1.0, 0], ['X4', 2, 1.0, 1.0, 0, 1.0, 0, 0, 0], ['U2', 8, 1.0, 2.0, 0, 0, -1.0, 0, 1.0]]

What should I do to get the matrix_aux_ord correctly, and with it to be able to get the matrix new_matrix which basically consists of a way to combine amber matrices, matrix and matrix_aux_ord?
