Python之2维list转置、旋转及其简单应用
给一个矩阵,顺时针旋转顺序输出其元素,例如:
对于矩阵:
[ 1, 2, 3 ]
[ 4, 5, 6 ]
[ 7, 8, 9 ]
输出为:
1,2,3,6,9,8,7,4,5
def transpose(matrix):
return zip(*matrix) def rotate(matrix):
return zip(*matrix)[::-1] def rotatePrint(matrix):
import copy
matrix = copy.deepcopy(matrix)
return matrix and list(matrix.pop(0)) + rotatePrint(rotate(matrix)) ######### Test ############# def printMatrix(matrix):
for row in matrix:
print ' '.join( str(i) for i in row) matrix = [
[1,2,3],
[4,5,6],
[7,8,9]] print 'original:'
printMatrix(matrix) print 'rotate print:',rotatePrint(matrix) print 'transpose:'
printMatrix( transpose(matrix) ) print 'rotate:'
printMatrix( rotate(matrix))
输出为:

Python之2维list转置、旋转及其简单应用的更多相关文章
- Python小代码_5_二维矩阵转置
使用列表推导式实现二维矩阵转置 matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] print(matrix) matrix_t = [[ro ...
- 【转载】ArcBall二维控制三维旋转
原文:http://oviliazhang.diandian.com/post/2012-05-19/40027878859 由于目前大多的显示器是二维的,要控制三维物体的旋转就显得不那么直接了.Ar ...
- Day1 老男孩python自动化运维课程学习笔记
2017年1月7日老男孩python自动化运维课程正式开课 第一天学习内容: 上午 1.python语言的基本介绍 python语言是一门解释型的语言,与1989年的圣诞节期间,吉多·范罗苏姆为了在阿 ...
- python自动化运维学习第一天--day1
学习python自动化运维第一天自己总结的作业 所使用到知识:json模块,用于数据转化sys.exit 用于中断循环退出程序字符串格式化.format字典.文件打开读写with open(file, ...
- 有关python下二维码识别用法及识别率对比分析
最近项目中用到二维码图片识别,在python下二维码识别,目前主要有三个模块:zbar .zbarlight.zxing. 1.三个模块的用法: #-*-coding=utf-8-*- import ...
- 从Scratch到Python——Python生成二维码
# Python利用pyqrcode模块生成二维码 import pyqrcode import sys number = pyqrcode.create('从Scratch到Python--Pyth ...
- python常用运维脚本实例
转载 file是一个类,使用file('file_name', 'r+')这种方式打开文件,返回一个file对象,以写模式打开文件不存在则会被创建.但是更推荐使用内置函数open()来打开一个文件 ...
- 转:python常用运维脚本实例
python常用运维脚本实例 转载 file是一个类,使用file('file_name', 'r+')这种方式打开文件,返回一个file对象,以写模式打开文件不存在则会被创建.但是更推荐使用内置函 ...
- Python自动化运维之路-01
python的主要应用 python的擅长领域 学python有没有前途?python的语言排名 语言选择 运维会了开发后可以干什么? python的最大优势就是什么都能做. 课程概述 毕业目标 周五 ...
随机推荐
- [svc]linux内核参数
内核参数 说明 net.ipv4.tcp_max_syn_backlog = 2048 增大队列SYN最大半连接数; 对于那些依然还未获得客户端确认的连接请求,需要保存在队列中最大数目.默认值是102 ...
- K8S 详细介绍
k8s的中文文档,参考地址:http://docs.kubernetes.org.cn/227.html
- python(45)内置函数:os.system() 和 os.popen()
os.system() 和 os.popen() 概述 os.popen() 方法用于从一个命令打开一个管道. 在Unix,Windows中有效 语法 popen()方法语法格式如下: os.pope ...
- 部署ArcGIS JS API 离线包(Tomcat与IIS)
http://www.cnblogs.com/ventlam/archive/2012/12/13/2815583.html
- uWSGI listen queue 队列溢出的问题
如果没有设置uwsgi的--listen,如果sysctl -a | grep net.core.somaxconn发现net.core.somaxconn=128. 那你使用uwsgi启动的服务,单 ...
- 【delphi】delphi操作sqlite3
SQLite SQLite是一个老牌的轻量级别的本地文件数据库,完全免费且开源,不需要安装,无须任何配置,当然,这样管理功能就不是很强大了,但是它的主要应用也是在本地数据库,可以说是最简单好用的嵌入式 ...
- Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明
https://www.sojson.com/blog/246.html ******************************************** Jackson 是 Spring ...
- cat正常,cat重定向到文件可能是乱码;解决办法
文件cat正常,cat重定向到文件可能是乱码:解决办法如下 .vimrc文件中增加如下两行 set fileencoding=utf8 set fileencodings=utf8 据一位老神医说,文 ...
- Leetcode:Interleaving String 解题报告
Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For ...
- Python字符串、时间戳、datetime时间相关转换
总结的时间转换函数 # datetime时间转为字符串 def Changestr(datetime1): str1 = datetime1.strftime('%Y-%m-%d %H:%M:%S') ...