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

作者:nillei 时间:2023-03-25 14:03:52 

如下所示:


# -*- 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

来源:https://blog.csdn.net/nillei/article/details/60962831

标签:python,打印,矩阵
0
投稿

猜你喜欢

  • WEB前端开发经验总结 Ⅰ

    2008-06-12 12:23:00
  • python使用matplotlib库生成随机漫步图

    2021-03-01 15:11:37
  • Python基于多线程实现ping扫描功能示例

    2023-08-02 17:30:09
  • NumPy-ndarray 的数据类型用法说明

    2023-07-09 22:01:40
  • 自动完成autoComplete

    2011-01-17 18:01:00
  • python绘图subplots函数使用模板的示例代码

    2023-05-23 06:05:01
  • 如何正确处理数据库中的Null

    2007-09-30 19:41:00
  • Python中的上下文管理器和with语句的使用

    2021-01-28 00:29:51
  • 使用postman进行接口自动化测试

    2022-11-09 08:36:35
  • Python中矩阵库Numpy基本操作详解

    2021-07-09 18:13:05
  • PHP桥接模式Bridge Pattern的优点与实现过程

    2023-05-25 06:53:44
  • 一文读懂navicat for mysql基础知识

    2024-01-23 00:46:25
  • zabbix监控MySQL主从状态的方法详解

    2024-01-22 00:29:24
  • 使用python flask框架开发图片上传接口的案例详解

    2021-12-26 05:54:30
  • python Selenium实现付费音乐批量下载的实现方法

    2023-02-28 17:26:58
  • vue中resetFields重置初始值不生效的原因详解

    2024-05-29 22:29:12
  • linux中用shell快速安装配置Go语言的开发环境

    2024-02-12 22:38:11
  • windows环境中利用celery实现简单任务队列过程解析

    2023-08-09 12:15:28
  • 利用Python计算圆周率π的实例代码

    2021-01-08 23:26:43
  • SQL Server格式转换函数Cast、Convert介绍

    2024-01-22 18:42:16
  • asp之家 网络编程 m.aspxhome.com