我如何更正我的代码,以便能够对其元素进行排序,根据这些元素,在最接近其子列表开头的元素中,规范向量的值等于1.0(忽略第一个子列表,这是带有标题的子列表,尽管这也会根据其他子列表中元素1.0的位置改变位置),从而保留?

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是它忘记了对标题或标题行['X4', 'U1', 'U2']进行排序,错误地打印了该矩阵

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

如果它保持一致性,而不是这个

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

然后,拥有这两个矩阵,构建名为new_matrix的新矩阵,其中我将在matrix前面添加一列,也就是说,我将向组成matrix行的每个子列表添加一个元素,向名为matrix的矩阵的第一个子列表添加一个元素,并在名为matrix的矩阵的其余子列表中添加一个‘X’作为第一个元素,并以有序的方式将名为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')

因此,最终得到的矩阵correct output,称为new_matrix,将如下所示:

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]]

我应该怎么做才能正确地得到matrix_aux_ord,并用它来得到矩阵new_matrix,它基本上包括一种组合琥珀矩阵matrixmatrix_aux_ord的方法?

推荐答案

你需要把这titles个和其他 list 放在一起.你可以用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]]

Python相关问答推荐

手动为pandas中的列上色

Python在通过Inbox调用时给出不同的响应

FastAPI:使用APIRouter路由子模块功能

Pandas使用过滤器映射多列

Polars -转换为PL后无法计算熵.列表

创建带有二维码的Flask应用程序,可重定向到特定端点

如何让我的Tkinter应用程序适合整个窗口,无论大小如何?

将DF中的名称与另一DF拆分并匹配并返回匹配的公司

将数据框架与导入的Excel文件一起使用

' osmnx.shortest_track '返回有效源 node 和目标 node 的'无'

删除字符串中第一次出现单词后的所有内容

运输问题分支定界法&

无法使用DBFS File API路径附加到CSV In Datricks(OSError Errno 95操作不支持)

如何创建一个缓冲区周围的一行与manim?

使用特定值作为引用替换数据框行上的值

幂集,其中每个元素可以是正或负""""

以逻辑方式获取自己的pyproject.toml依赖项

寻找Regex模式返回与我当前函数类似的结果

如何在两列上groupBy,并使用pyspark计算每个分组列的平均总价值

为什么调用函数的值和次数不同,递归在代码中是如何工作的?