python 顺时针打印矩阵的超简洁代码

yipeiwu_com6年前Python基础

如下所示:

# -*- coding:utf-8 -*-
class Solution:
  # matrix类型为二维列表,需要返回列表
  def printMatrix(self, matrix):
    # write code here
    res=[]
    n=len(matrix)
    m=len(matrix[0])
    if m==1 and n==1:
      res=[matrix[0][0]]
      return res
    else:
      for o in range((min(m,n)+1)//2):
        [res.append(matrix[o][i]) for i in range(o,m-o)]
        [res.append(matrix[j][m-o-1]) for j in range(o,n-o) if matrix[j][m-o-1] not in res]
        [res.append(matrix[n-o-1][k]) for k in range(m-1-o,o-1,-1) if matrix[n-o-1][k] not in res]
        [res.append(matrix[l][o]) for l in range(n-1-o,o-1,-1) if matrix[l][o] not in res]
      return res

以上这篇python 顺时针打印矩阵的超简洁代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

django与vue的完美结合_实现前后端的分离开发之后在整合的方法

django与vue的完美结合_实现前后端的分离开发之后在整合的方法

最近接到一个任务,就是用django后端,前段用vue,做一个普通的简单系统,我就是一搞后端的,听到vue也是比较震惊,之前压根没接触过vue. 看了vue的一些文档,还有一些项目,先说...

Python画图高斯分布的示例

如下所示: import matplotlib.pyplot as plt import numpy as np import math def gaussian(sigma, x,...

Apache,wsgi,django 程序部署配置方法详解

Apache,wsgi,django 程序部署配置方法详解

本文实例讲述了Apache,wsgi,django 程序部署配置方法。分享给大家供大家参考,具体如下: 前面写过一篇文章,ngixn,uwsgi,django,python 环境配置,有...

Python的collections模块中namedtuple结构使用示例

namedtuple 就是命名的 tuple,比较像 C 语言中 struct。一般情况下的 tuple 是 (item1, item2, item3,...),所有的 item 都只能...

python类中super()和__init__()的区别

单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(self): print 'Base create'...