当前位置:首页 > 编程技术 > 正文内容

使用 Python 按行和按列对矩阵进行排序

yc8881个月前 (02-21)编程技术26

使用 Python 按行和按列对矩阵进行排序

在本文中,我们将学习一个 python 程序来按行和按列对矩阵进行排序。

假设我们采用了一个输入的 MxM 矩阵。我们现在将使用嵌套的 for 循环对给定的输入矩阵进行逐行和按列排序。

算法(步骤)

以下是执行所需任务要遵循的算法/步骤。−

  • 创建一个函数sortingMatrixByRow()来对矩阵的每一行进行排序,即通过接受输入矩阵m(行数)作为参数来逐行排序

  • 在函数内部,使用 for 循环遍历矩阵的行。

  • 使用另一个嵌套的 for 循环遍历当前行的所有列。

  • 使用 if 条件语句检查当前元素是否大于下一个元素。

  • 如果条件为 true,则使用临时变量交换元素

  • 创建另一个函数 transposeMatrix() 通过接受输入矩阵 m(行数)作为参数来获取矩阵的转置。

  • 使用 for 循环遍历矩阵的行。

  • 使用另一个嵌套的 for 循环遍历窗体(行 +1)列到列的末尾。

  • 将当前行、列元素与列、行元素交换。

  • 创建一个函数 sortMatrixRowandColumn() 通过接受输入矩阵 m(行数)作为参数来对矩阵行和列进行排序。

  • 在函数内部,调用上面定义的 sortingMatrixByRow() 函数对输入矩阵的行进行排序。

    站长百科网
  • 调用上面定义的转置矩阵() 函数来获取输入矩阵的转置。

  • 再次通过调用上面定义的 sortingMatrixByRow() 函数对输入矩阵的行进行排序。

  • 再次通过调用上面定义的转置矩阵() 函数来获取输入矩阵的转置。

  • 创建一个函数 printingMatrix() 通过使用嵌套的 for 循环遍历矩阵的行和列来打印矩阵。

  • 创建一个变量来存储输入矩阵

  • 创建另一个变量来存储输入 m(行数)值

  • 调用上面定义的 printingMatrix() 函数来打印输入矩阵。

  • 调用上面定义的sortMatrixRowandColumn()函数,方法是将输入矩阵,m值传递给它,对矩阵行和列进行排序。

  • 通过调用上面定义的 printingMatrix() 函数按行和按列排序后打印生成的输入矩阵。

以下程序使用嵌套的 for 循环返回给定输入矩阵的按行和按列排序的矩阵 -

# creating a function for sorting each row of matrix row-wise def sortingMatrixByRow(inputMatrix, m):    #  traversing till the length of rows of a matrix       for p in range(m):                # Sorting the current row          for q in range(m-1):             # checking whether the current element is greater than the next element                if inputMatrix[p][q] >inputMatrix[p][q + 1]:                   # swapping the elements using a temporary variable                   # if the condition is true                   tempVariable = inputMatrix[p][q]                   inputMatrix[p][q] = inputMatrix[p][q + 1]                   inputMatrix[p][q + 1] = tempVariable # creating a function to get the transpose of a matrix # by accepting the input matrix, m values as arguments def transposeMatrix(inputMatrix, m):    # traversing through the rows of a matrix       for p in range(m):          # Traversing from row +1 column to last column          for q in range(p + 1, m):             # Swapping the element at index (p,q) with (q,p)             temp = inputMatrix[p][q]             inputMatrix[p][q] = inputMatrix[q][p]             inputMatrix[q][p] = temp # creating a function for sorting the matrix rows column-wise def sortMatrixRowandColumn(inputMatrix, m):    # sorting the rows of an input matrix by    # calling the above defined sortingMatrixByRow() function    sortingMatrixByRow(inputMatrix, m)    # getting the transpose of an input matrix by    # calling the above defined transposeMatrix() function    transposeMatrix(inputMatrix, m)    # once again sorting the rows of an input matrix by    # calling the above defined sortingMatrixByRow() function    sortingMatrixByRow(inputMatrix, m)    # once again getting the transpose of an input matrix(So we sorted the columns)    transposeMatrix(inputMatrix, m) # creating a function to print the matrix def printingMatrix(inputMatrix, rows):    # Traversing in the rows of the input matrix    for i in range(rows):       # Traversing in the columns corresponding to the current row          # of the input matrix          for j in range(rows):             print(inputMatrix[i][j], end=" ")          # Printing a new line to separate the rows          print() # input matrix inputMatrix = [[2, 6, 5],                [1, 9, 8],                [7, 3, 10]] # input m value representing 3x3 matrix # (dimensions) m = 3 print("Input Matrix:") # printing the input matrix by calling the above # printingMatrix() function printingMatrix(inputMatrix, m) # calling the above defined sortMatrixRowandColumn() function # by passing the input matrix, m values to it to # sort the matrix row and column-wise sortMatrixRowandColumn(inputMatrix, m) # printing the input matrix after sorting row and column-wise # by calling the above printingMatrix() function print("Input Matrix after sorting row and column-wise:") printingMatrix(inputMatrix, m)

输出

在执行时,上述程序将生成以下输出 -

Input Matrix: 2 6 5  1 9 8  7 3 10  Input Matrix after sorting row and column-wise: 1 5 6  2 7 9  3 8 10

时间复杂度 − O(n^2 log2n)

辅助空间 − O(1)

结论

在本文中,我们学习了如何使用 Python 对给定的矩阵进行行和列排序。此外,我们还学习了如何转置给定的矩阵,以及如何使用嵌套的 for 循环(而不是使用内置的 sort() 方法)按行对矩阵进行排序。


标签: Python

“使用 Python 按行和按列对矩阵进行排序” 的相关文章

【说站】laravel实现自定义404页面并给页面传值

【说站】laravel实现自定义404页面并给页面传值

以 laravel5.8 为例,虽然有自带的404页面,但太简单,我们更希望能自定义404页面,将用户留在站点。实现的方式很简单,将自定义的视图文件命名为 404.blade.php,并放到 reso...

【说站】电脑安装MySQL时出现starting the server失败原因及解决方案

【说站】电脑安装MySQL时出现starting the server失败原因及解决方案

今天在安装MySQL时出现starting the server失败,经过查询分析得出以下结论,记录一下操作步骤。原因分析:如果电脑是第一次安装MySQL,一般不会出现这样的报错。如下图所示。star...

【说站】C#在PDF中添加墨迹注释Ink Annotation的步骤详解

【说站】C#在PDF中添加墨迹注释Ink Annotation的步骤详解

PDF中的墨迹注释(Ink Annotation),表现为徒手涂鸦式的形状;该类型的注释,可任意指定形状顶点的位置及个数,通过指定的顶点,程序将连接各点绘制成平滑的曲线。下面,通过C#程序代码介绍如何...

【说站】linux中redis如何以redis用户重启?

【说站】linux中redis如何以redis用户重启?

通过上图我们可以看到,目前状态是已经以 redis 用户启动着,我想修改下 redis 的密码,然后怎么以 redis 用户重启呢?redis 是 nologin 用户,不能通过 su redis 切...

【说站】使用systemctl配置dnspod-shell实现ddns

【说站】使用systemctl配置dnspod-shell实现ddns

这个是毛子路由器上用的脚本,由于碳云的nat服务器公网IP不断的变,因此只好通过ddns来稳定连接nat服务器了。顺便水一篇文章,大家新年快乐。使用前需要将域名添加到 DNSPod 中,并添加一条A记...

【说站】Python获取最新疫情数据实现动态地图实时展示各地情况

【说站】Python获取最新疫情数据实现动态地图实时展示各地情况

疫情降临转眼已经第三年了,时间过得真快,愿疫情早点结束,世界不再多灾多难。最近疫情稍微好转一些了,所以咱们获取一下最新的疫情数据,做个可视化地图看看。效果展示获取到的数据咱们保存到表格可视化地图颜色是...