当前位置: 首页 > 图灵资讯 > 行业资讯> python如何打印矩阵

python如何打印矩阵

发布时间:2024-06-27 19:25:20

1、使用*解包,zip压缩,zip变成zip类型,zip将原始矩阵从第一列开始,将每一列打包成一个元祖,将元祖强转为list,以达到矩阵转移的效果。

defspiralOrder(matrix):
res=[]
whilematrix:
print('matrix=',matrix)
res+=matrix.pop(0)
matrix=list(zip(*matrix))[::-1]
returnres

2、顺时针旋转,得到的值可以逆转。

defanti(matrix):
res=[]
res+=matrix.pop(0)[::-1]
whilematrix:
matrix=matrix[::-1]
matrix=list(zip(*matrix))
print('msss=',matrix)
res+=list(matrix.pop(0))[::-1]
returnres

以上是python打印矩阵的方法,希望对大家有所帮助。更多Python学习指导:python基础教程

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09