给一个矩阵,顺时针旋转顺序输出其元素,例如:

对于矩阵:

[ 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转置、旋转及其简单应用的更多相关文章

  1. Python小代码_5_二维矩阵转置

    使用列表推导式实现二维矩阵转置 matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] print(matrix) matrix_t = [[ro ...

  2. 【转载】ArcBall二维控制三维旋转

    原文:http://oviliazhang.diandian.com/post/2012-05-19/40027878859 由于目前大多的显示器是二维的,要控制三维物体的旋转就显得不那么直接了.Ar ...

  3. Day1 老男孩python自动化运维课程学习笔记

    2017年1月7日老男孩python自动化运维课程正式开课 第一天学习内容: 上午 1.python语言的基本介绍 python语言是一门解释型的语言,与1989年的圣诞节期间,吉多·范罗苏姆为了在阿 ...

  4. python自动化运维学习第一天--day1

    学习python自动化运维第一天自己总结的作业 所使用到知识:json模块,用于数据转化sys.exit 用于中断循环退出程序字符串格式化.format字典.文件打开读写with open(file, ...

  5. 有关python下二维码识别用法及识别率对比分析

    最近项目中用到二维码图片识别,在python下二维码识别,目前主要有三个模块:zbar .zbarlight.zxing. 1.三个模块的用法: #-*-coding=utf-8-*- import ...

  6. 从Scratch到Python——Python生成二维码

    # Python利用pyqrcode模块生成二维码 import pyqrcode import sys number = pyqrcode.create('从Scratch到Python--Pyth ...

  7. python常用运维脚本实例

    转载  file是一个类,使用file('file_name', 'r+')这种方式打开文件,返回一个file对象,以写模式打开文件不存在则会被创建.但是更推荐使用内置函数open()来打开一个文件 ...

  8. 转:python常用运维脚本实例

    python常用运维脚本实例 转载  file是一个类,使用file('file_name', 'r+')这种方式打开文件,返回一个file对象,以写模式打开文件不存在则会被创建.但是更推荐使用内置函 ...

  9. Python自动化运维之路-01

    python的主要应用 python的擅长领域 学python有没有前途?python的语言排名 语言选择 运维会了开发后可以干什么? python的最大优势就是什么都能做. 课程概述 毕业目标 周五 ...

随机推荐

  1. C#中计算时间差

    问题: startTime = DateTime.Now;            -----------slExecutedTime.Text = (DateTime.Now - startTime) ...

  2. Socket网络编程--Libev库学习(3)

    这一小节继续讲解各个观察器(Watcher). 上一小节已经讲解了ev_io(IO可读可写观察器),ev_stat(文件属性变化观察器),ev_signal(信号处理观察器),ev_timer(定时器 ...

  3. Cowboy实例

    这个例子主要是用cocos2d-x引擎自带的资源 cocos2d-x-2.2.2\samples\Cpp\TestCpp\Resources\armature 新建工程之后 #include &quo ...

  4. java park unpark

    https://blogs.oracle.com/dave/a-race-in-locksupport-park-arising-from-weak-memory-models https://blo ...

  5. studying Bitcoin

    https://github.com/bitcoinbook/bitcoinbook/blob/develop/book.asciidoc https://github.com/bitcoin/bip ...

  6. 非常详尽的 Shiro 架构解析

    Shiro是什么? Apache Shiro是一个强大而灵活的开源安全框架,它干净利落地处理身份认证,授权,企业会话管理和加密. Apache Shiro的首要目标是易于使用和理解.安全有时候是很复杂 ...

  7. [Windows Azure] Windows Azure SQL Database library

    Microsoft Windows Azure SQL Database extends SQL Server capabilities to the cloud. SQL Database offe ...

  8. zsh与oh-my-zsh

    在开始今天的 MacTalk 之前,先问两个问题吧: 1.相对于其他系统,Mac 的主要优势是什么?2.你们平时用哪种 Shell?…… 第一个童靴可以坐下了,Mac 的最大优势是 GUI 和命令行的 ...

  9. 使用PostgreSQL遇到的一个问题[column does not exist]字段不存在:

    表结构: 在我上面的表结构中,明明有一个叫做"fromdeviceId"的字段,但是查询的时候却提示找不到该字段: 仔细观察错误信息,发现,我的字段明明是有一个大写字母(fromd ...

  10. python unicode to str and str to unicode

    @staticmethod def unicode2str(p_unicode): v = p_unicode.encode('unicode-escape').decode('string_esca ...