Python程序以蛇形模式打印矩阵
在本文中,我们将学习一个Python程序,以蛇形模式打印矩阵。
Assume we have taken the n x n matrix. We will now print the input matrix in a snake pattern using the below-mentioned methods.
Methods Used
The following are the various methods used to accomplish this task −
使用嵌套的for循环
Reversing Alternate Rows Using Slicing
直觉
我们将迭代遍历矩阵的所有行。对于每一行,我们将检查它是偶数还是奇数。如果行是偶数,则从左到右打印矩阵,否则从右到左打印矩阵。
Method 1: Using the nested for loop
算法(步骤)
以下是执行所需任务的算法/步骤。−
Create a variable to store the number of rows of a matrix.
Create another variable to store the number of columns of a matrix.
创建一个函数printSnakePattern(),通过接受输入矩阵作为参数以蛇形模式打印矩阵。
Use the global keyword to make the rows and columns variables global.
使用for循环遍历矩阵的行。
使用if条件语句来检查当前行号是否为偶数。
Use another nested for loop to traverse through all the columns of the current row if the condition is true.
Print the matrix row from left to right if the current row is even.
否则,如果当前行是奇数,则从右到左打印矩阵行。
Create a variable to store the input matrix and print the given matrix.
通过将输入矩阵作为参数调用上述定义的 printSnakePattern() 函数。
Example
以下程序使用嵌套的for循环以蛇形模式打印输入矩阵:
# initializing the number of rows of the matrix rows = 4 # initializing the number of columns of the matrix columns = 4 # creating a function for printing the matrix in # snake pattern accepting the input matrix as an argument. def printSnakePattern(inputMatrix): # making the rows and columns variables global global rows, columns # traversing through the rows of a matrix for m in range(rows): # checking whether the current row number is even if m % 2 == 0: # traversing through all the columns of the current row for n in range(columns): # printing from left to right if the current row is even print(inputMatrix[m][n], end=" ") # Else, printing from right to left if the current row is even else: # traversing from the end of the columns for n in range(columns - 1, -1, -1): print(inputMatrix[m][n], end=" ") # input matrix inputMatrix = [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] print("The Given Matrix is :") print(inputMatrix) # calling the above-defined printSnakePattern function # by passing the input matrix as an argument. print("Snake Pattern of the given Matrix is:") printSnakePattern(inputMatrix)
输出
On executing, the above program will generate the following output −
The Given Matrix is : [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] Snake Pattern of the given Matrix is: 3 4 5 6 80 60 40 10 1 9 7 8 15 14 20 40
方法2:使用切片反转交替行
slicing is a frequent practice and the one that programmers utilize the most to solve problems effectively. Consider a Python list. You must slice a list to access a range of list elements. The use of the colon(:), a simple slicing operator, is one method for accomplishing this.
Syntax
[start:stop:step]
参数
start − 从哪里开始的索引
end − ending index
步骤 − 在i之间要跳跃的次数,即步长
Example
以下程序使用切片以蛇形模式打印输入矩阵。
# input matrix inputMatrix = [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] # initializing the number of rows of a matrix rows = 4 # initializing the number of columns of a matrix columns = 4 # creating a function for printing the matrix in # snake pattern accepting the input matrix as an argument. def printSnakePattern(inputMatrix): # making the rows and columns variables global global rows, columns # traversing through the rows of a matrix for m in range(rows): # checking whether the current row number is even if m % 2 != 0: # Reversing the row using reverse slicing inputMatrix[m] = inputMatrix[m][::-1] # traversing through the rows of a matrix for m in range(rows): # traversing through all the columns of the current row for n in range(columns): # printing the corresponding element print(inputMatrix[m][n], end=' ') # input matrix inputMatrix = [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] print("The Given Matrix is :") print(inputMatrix) # calling the above-defined printSnakePattern function # by passing the input matrix as an argument. print("Snake Pattern of the given Matrix is:") printSnakePattern(inputMatrix)
输出
On execution, the above program will generate the following output −
The Given Matrix is : [[3, 4, 5, 6], [10, 40, 60, 80], [1, 9, 7, 8], [40, 20, 14, 15]] The Snake Pattern of the given Matrix is: 3 4 5 6 80 60 40 10 1 9 7 8 15 14 20 40
Conclusion
In this article, we learned how to print the given matrix in snake form using two different methods. We learned how to use the global keyword to make variables global. We also learned how to reverse any iterable, including a list, tuple, string, etc. via reverse slicing.
以上是Python程序以蛇形模式打印矩阵的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

如果在打开一份需要打印的文件时,在打印预览里我们会发现表格框线不知为什么消失不见了,遇到这样的情况,我们就要及时进行处理,如果你的打印文件里也出现了此类的问题,那么就和小编一起来学习下边的课程吧:excel打印表格框线消失怎么办?1、打开一份需要打印的文件,如下图所示。 2、选中所有需要的内容区域,如下图所示。 3、单击鼠标右键,选择“设置单元格格式”选项,如下图所示。 4、点击窗口上方的“边框”选项,如下图所示。 5、在左侧的线条样式中选择细实线图样,如下图所示。 6、选择“外边框”

本文将介绍如何解决在MicrosoftWord中出现的内存或磁盘空间不足以重新分页或打印文档的问题。这种错误通常会在用户尝试打印Word文档时出现。如果您遇到类似的错误,请参考本文提供的建议进行解决。内存或磁盘空间不足,无法重新分页或打印此文档Word错误解决MicrosoftWord打印错误“没有足够内存或磁盘空间重新分页或打印文档”的方法。更新MicrosoftOffice关闭占用内存的应用程序更改您的默认打印机在安全模式下启动Word重命名NorMal.dotm文件将Word文件保存为另一

在这个数字化的世界中,打印页面的需求并没有消失。尽管您可能认为在计算机上保存内容并直接发送到打印机更为便捷,但是您同样可以在iPhone上完成相同的操作。通过iPhone的相机,您可以拍摄照片或文档,并且还可以直接存储文件以便随时打印。这样一来,您可以快速方便地将您所需的信息实体化,并将其保存在纸质文档中。无论是在工作中还是日常生活中,iPhone为您提供了一个便携式的打印解决方案。以下帖子将帮助您了解如果您希望使用iPhone在打印机上打印页面,您需要了解的所有信息。从iPhone打印:要求苹

如果您在Windows11/10中无法使用截图工具进行打印,可能是由于系统文件损坏或驱动程序问题导致的。本文将为您提供解决此问题的方法。在Windows11/10中无法从截图工具打印如果您无法从Windows11/10中的SnippingTool打印,请使用这些修复程序:重新启动PC打印机清除打印队列更新打印机和显卡驱动程序修复或重置剪裁工具运行SFC和DISM扫描使用PowerShell命令卸载并重新安装截图工具。我们开始吧。1]重新启动您的PC和打印机重新启动电脑和打印机有助于消除暂时的故障

错误地打印了一个大文件?需要停止或暂停打印以节省墨水和纸张吗?在许多情况下,您可能需要暂停Windows11设备上正在进行的打印作业。如何在Windows11中暂停打印?在Windows11中,暂停打印会暂停打印作业,但并不会取消打印任务。这为用户提供了更灵活的控制权。有三种方法可以实现这一点:使用任务栏暂停打印使用Windows设置暂停打印使用控制面板打印现在,让我们来详细看看这些。1]使用任务栏打印右键单击任务栏上的打印队列通知。单击打开所有活动打印机选项。在这里,右击打印作业并选择全部暂停

如果您发现在使用Word打印邮件合并文档时出现空白页,这篇文章将对您有所帮助。邮件合并是一项便捷的功能,让您能够轻松创建个性化文档并发送给多个收件人。在MicrosoftWord中,邮件合并功能备受推崇,因为它能够帮助用户节省手动为每个收件人复制相同内容的时间。为了打印邮件合并文档,您可以转到邮件选项卡。但是一些Word用户反映,在尝试打印邮件合并文档时,打印机会打印空白页或根本不打印。这可能是由于格式设置不正确或打印机设置问题。尝试检查文档和打印机设置,确保打印前预览文档,以确保内容正确。如果

Outlook是功能最丰富的电子邮件客户端之一,已成为专业交流不可或缺的工具。其中一个挑战是在Outlook中同时打印所有附件。通常需要逐个下载附件才能打印,但如果想一次性打印所有内容,这就是大多数人遇到的问题。如何打印Outlook中的所有附件尽管大部分信息是在Outlook应用程序中在线维护的,但有时需要将信息打印出来备份。必须亲自签署文件,以满足合同、政府表格或家庭作业等法律要求。有几种方法可以让您一次单击打印Outlook中的所有附件,而不是逐个打印。让我们详细地看看每一个。Outloo

如何在Vue中实现打印功能,需要具体代码示例Vue.js是一种用于构建用户界面的渐进式JavaScript框架。在许多Web应用程序中,打印功能是非常重要的一部分。本文将介绍如何在Vue中实现打印功能,并提供具体的代码示例。在Vue中实现打印功能,首先要明确打印的内容是什么。通常,我们会将要打印的内容放在一个HTML元素中,例如一个div。然后,通过Jav
