Python实现循环打印举证对角线数据列表
菜鸟方法,不喜勿喷,欢迎指正
测试矩阵:
# 测试矩阵
_list = [[ 1, 2, 3, 4, 5, 6],[ 7, 8, 9, 10, 11, 12],[13, 14, 15, 16, 17, 18],[19, 20, 21, 22, 23, 24],[25, 26, 27, 28, 29, 30],[31, 32, 33, 34, 35, 36],
]
- left -> right
# left -> right
x, y = 0, len(_list) - 1
left_to_right_list = []for i in range(len(_list) * 2 - 1):if (x == 0 and y == len(_list) - 1) or (x == len(_list) - 1 and y == 0):left_to_right_list.append([_list[x][y]])else:temp_list = []if i <= len(_list) - 1:temp_y = ytemp_x = xwhile temp_y <= len(_list) - 1:temp_list.append(_list[temp_x][temp_y])temp_x += 1temp_y += 1left_to_right_list.append(temp_list)else:x += 1 # 从第二行开始y = 0 # 从第一列开始temp_x = xtemp_y = ywhile temp_x <= len(_list) - 1:temp_list.append(_list[temp_x][temp_y])temp_x += 1temp_y += 1left_to_right_list.append(temp_list)y -= 1for i in left_to_right_list:print(i)
## 结果 ##
# [6]
# [5, 12]
# [4, 11, 18]
# [3, 10, 17, 24]
# [2, 9, 16, 23, 30]
# [1, 8, 15, 22, 29, 36]
# [7, 14, 21, 28, 35]
# [13, 20, 27, 34]
# [19, 26, 33]
# [25, 32]
# [31]
- right -> left
# right -> left
x, y = 0, 0
right_to_left_list = []
for i in range(len(_list) * 2 - 1):if x == y:right_to_left_list.append([_list[x][y]])y += 1else:temp_list = []if i <= len(_list) - 1:# i小于等于列表长度时x为0temp_y = itemp_x = xwhile temp_y >= 0:temp_list.append(_list[temp_x][temp_y])temp_x += 1temp_y -= 1right_to_left_list.append(temp_list)y += 1else:x += 1# i大于列表长度时y为列表长度temp_y = len(_list) - 1temp_x = x # 从第二行开始while temp_x <= len(_list) - 1:temp_list.append(_list[temp_x][temp_y])temp_x += 1temp_y -= 1right_to_left_list.append(temp_list)for i in right_to_left_list:print(i)
## 结果 ##
# [1]
# [2, 7]
# [3, 8, 13]
# [4, 9, 14, 19]
# [5, 10, 15, 20, 25]
# [6, 11, 16, 21, 26, 31]
# [12, 17, 22, 27, 32]
# [18, 23, 28, 33]
# [24, 29, 34]
# [30, 35]
# [36]
Python实现循环打印举证对角线数据列表相关推荐
- python for 循环中使用 remove 删除列表中的元素
python for 循环中使用 remove 删除列表中的元素 错误的代码 # !/usr/bin/python # encoding: utf-8 # -*- coding: utf8 -*- o ...
- [转载] python while循环 打印菱形
参考链接: Python while循环 . 代码如下: .
- python爬虫循环表格xpath_python爬虫数据解析之xpath
xpath是一门在xml文档中查找信息的语言.xpath可以用来在xml文档中对元素和属性进行遍历. 在xpath中,有7中类型的节点,元素,属性,文本,命名空间,处理指令,注释及根节点. 节点 首先 ...
- 用python找出所有三位数中的水仙花数_python使用循环打印所有三位数水仙花数的实例...
首先水仙花数是什么? 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI).自恋数.自幂数.阿姆斯壮数或阿 ...
- python打印所有的水仙花数_python使用循环打印所有三位数水仙花数的实例
首先水仙花数是什么? 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI).自恋数.自幂数.阿姆斯壮数或阿 ...
- python三位数水仙花数计算_python使用循环打印所有三位数水仙花数的实例
首先水仙花数是什么? 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI).自恋数.自幂数.阿姆斯壮数或阿 ...
- Python 中使用 for、while 循环打印杨辉三角练习(列表索引练习)。
Python中使用for while循环打印杨辉三角练习(列表索引练习). 杨辉三角是一个由数字排列成的三角形数表,一般形式如下: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 ...
- python循环经典例题_python练习题:循环打印嵌套列表
好久没写博文了,添加一个练习题,选自<head_first_python>~~ python列表:以中括号开始和结束"[]":列表项以逗号","分隔 ...
- 将列表打印为表格数据
本文翻译自:Printing Lists as Tabular Data I am quite new to Python and I am now struggling with formattin ...
最新文章
- 【Android WebSocket】Android 端 WebSocket 基本用法 ( 添加依赖和权限 | 创建 WebSocketClient 客户端类 | 建立连接并发送消息 )
- go给Linux安装mysql_在Linux上安装Go语言开发包
- MSP430G2553需要注意的一些参数
- 数学学习笔记-三角函数
- Android Studio 1.1.0汉化初步出炉!
- bootstrap你让前端小狮子们又喜又恨
- Apache mod_rewrite规则重写的标志一览
- 问题解决,心情不错:)
- TCP编程、UDP编程
- 经典卷积神经网络——VGG16
- Linux -shell 基础
- 电脑无线网络(WiFi)密码查看(cmd命令控制符)
- 极智开发 | ubuntu 安装有线网卡驱动
- 高阳:我是黑客我怕谁
- Firefoo:Firebase Cloud Firestore——GUI工具
- python海龟编辑器中询问并获得输入咱弄出来的积木_海龟Python编辑器下载|海龟编辑器下载 0.8.0_咕噜下载...
- MTK工具sigma的使用
- 爱情,你究竟是什么模样?
- 通过C语言程序修改控制台的背景和前景颜色
- 饿了么并入口碑,一场“非典型”新零售大战的开始