Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按行和排序矩阵_Python_Algorithm_Sorting_Matrix - Fatal编程技术网

Python 按行和排序矩阵

Python 按行和排序矩阵,python,algorithm,sorting,matrix,Python,Algorithm,Sorting,Matrix,我一直在寻找解决这个问题的方法。我是编程新手,希望有人能带我通过思考过程来解决这个问题 用Python编写一个程序,对于随机输入图G={V,E},按度的降序对V={1,…,n}中的所有顶点进行稳定排序 设V={1,2,3,4,5}和 因此,输出由B={4,2,3,5,1}给出。尝试以下方法: matrix = [[0, 0, 0, 1, 0], [0, 0, 1, 1, 1], [0, 1, 0, 1, 0], [1, 1, 1, 0, 1], [0, 1, 0, 1, 0]] matrix

我一直在寻找解决这个问题的方法。我是编程新手,希望有人能带我通过思考过程来解决这个问题

用Python编写一个程序,对于随机输入图G={V,E},按度的降序对V={1,…,n}中的所有顶点进行稳定排序

设V={1,2,3,4,5}和

因此,输出由B={4,2,3,5,1}给出。

尝试以下方法:

matrix = [[0, 0, 0, 1, 0], [0, 0, 1, 1, 1], [0, 1, 0, 1, 0], [1, 1, 1, 0, 1], [0, 1, 0, 1, 0]]

matrix.sort(key=sum, reverse=True)
print(matrix)
更新: 这是您期望的输出形式:

matrix = [[0, 0, 0, 1, 0], [0, 0, 1, 1, 1], [0, 1, 0, 1, 0], [1, 1, 1, 0, 1], [0, 1, 0, 1, 0]]
count_ones = [(i + 1, item.count(1)) for i, item in enumerate(matrix)]
count_ones.sort(key=lambda x: x[1], reverse=True)
result = [i[0] for i in count_ones]
print(result)

这是家庭作业/考试吗?这是一道我想理解的老试题@willemvanonsem谢谢这个问题,我已经更新了我的答案。我正在寻找原始索引作为我的输出,所以{4,2,3,5,1}。当调用matrix.sort()时,我怎么能不丢失这个呢@请看我的最新答案谢谢,这很有效