英文:
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
问题
Here is the corrected code to achieve the desired matrix_aux_ord
and new_matrix
:
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]
# Add the column title or header 'X' to the front of the first sublist of matrix
matrix[0].insert(0, 'X')
# Build the new_matrix by combining matrix and matrix_aux_ord
new_matrix = [[matrix[0][j]] + [matrix_aux_ord[i][j-1] if j > 0 else matrix[i+1][0] for j in range(len(matrix[0]))] for i in range(len(matrix_aux_ord))]
print(new_matrix)
This code will give you the correct output for new_matrix
as specified in your question.
英文:
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
?
答案1
得分: 0
你需要将titles
与其他列表一起排序。你可以使用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)]
要将标题添加到matrix
中,请使用列表推导式。
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]]
英文:
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]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论