python实现二维数组的对角线遍历
作者:zzZ_CMing 时间:2023-05-15 06:08:01
本文实例为大家分享了python实现二维数组的对角线遍历,供大家参考,具体内容如下
第一种情况:从左上角出发,右下角结束
要完成的事情,就像下图:
话不多说,直接上Python实现代码与结果展示:
# 输出遍历的索引与其对应的值
A = [[1,2,3],
[4,5,6],
[7,8,9]]
n = len(A)
for i in range(n+n-1):
for j in range(i+1):
k = i-j
if k<n and k>=0 and j<n:
print("对应索引:",j,k, " 对应值:",A[j][k])
第二种情况:从右上角出发,左下角结束
情况如下:
原以为这一种的解决方式会和上一种的一样的简单,但是尝试一下失败了,只有一个“曲线救国”的解决办法:依据索引关系输出结果
自己的Python实现代码与结果展示:
A = [[1,2,3],
[4,5,6],
[7,8,9]]
# 得到下标索引
size = len(A)
list_i = [] # 第一索引
list_j = [] # 第二索引正序
l1 = [] # 第二索引逆序
for m in range(2*size-1):
for n in range(m+1):
k = m-n
if k<size and k>=0 and n<size:
list_i.append(n)
list_j.append(k)
print("第一索引i:\n", list_i)
print("第二索引j:")
for i in range(len(list_j)):
a = list_j.pop()
l1.append(a)
print(l1, "\n"*2, "输出结果如下:")
for i in range(len(list_i)):
print(A[list_i[i]][l1[i]], end= " ")
前辈jiaobuchong的Python实现代码与结果展示:
arr1 = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
for each_arr in arr1:
print(each_arr)
tem_arr = [] # 用来记录数组值
rows = len(arr1)
cols = len(arr1[0])
def isValidIndex(x, n):
return (x >= 0 and x < n)
# 每一行的每个值的数组下标的差都一样,
for i in range(cols * 2 - 1): # 共输出 cols * 2 - 1 行
diff = cols - i - 1 # 每一行的差
for j in range(cols): # 数组中每一个值的下标范围是0到cols
k = j - diff # 通过一个下标值计算另一个下标值
if isValidIndex(k, rows): # 剩下就是判断这些下标值是否满足当前的情况, 这一步不怎么好理解
print(arr1[k][j], ' ', end='')
print()
来源:https://blog.csdn.net/zzZ_CMing/article/details/81035354
标签:python,二维数组,遍历
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈python爬虫使用Selenium模拟浏览器行为
2021-04-23 22:26:46
![](https://img.aspxhome.com/file/2023/8/127148_0s.png)
十行Python代码制作一个视频倒放神器
2022-02-03 13:40:15
![](https://img.aspxhome.com/file/2023/4/68854_0s.gif)
Python实现html转换为pdf报告(生成pdf报告)功能示例
2023-11-07 02:31:17
JS简单实现无缝滚动效果实例
2024-04-16 09:34:52
![](https://img.aspxhome.com/file/2023/1/136591_0s.png)
如何通过Python收集MySQL MHA 部署及运行状态信息的功能
2024-01-13 22:14:44
![](https://img.aspxhome.com/file/2023/3/111363_0s.png)
Python中OpenCV实现查找轮廓的实例
2023-12-05 05:26:24
![](https://img.aspxhome.com/file/2023/4/112414_0s.png)
Python中利用Scipy包的SIFT方法进行图片识别的实例教程
2023-02-14 17:15:18
![](https://img.aspxhome.com/file/2023/5/72225_0s.jpg)
python中将两组数据放在一起按照某一固定顺序shuffle的实例
2022-02-13 12:15:14
Django中传递参数到URLconf的视图函数中的方法
2021-09-16 23:46:33
python基础教程之分支、循环简单用法
2021-05-30 08:51:48
python获取http请求响应头headers中的数据的示例
2023-06-30 14:49:02
![](https://img.aspxhome.com/file/2023/1/133911_0s.png)
python 装饰器重要在哪
2023-12-30 12:13:29
Python中__init__和__new__的区别详解
2023-09-24 13:14:17
python实现文本界面网络聊天室
2022-04-13 19:43:54
![](https://img.aspxhome.com/file/2023/2/112452_0s.png)
asp连接access数据库表代码实例
2008-04-13 06:18:00
Node.js 的异步 IO 性能探讨
2024-05-13 09:58:15
mysql 查询当天、本周,本月,上一个月的数据
2024-01-27 02:10:53
Anaconda配置pytorch-gpu虚拟环境的图文教程
2022-06-15 14:42:20
![](https://img.aspxhome.com/file/2023/4/117384_0s.jpg)
关于浮动的前世今生
2009-08-19 18:51:00
![](https://img.aspxhome.com/file/UploadPic/20098/19/01-12s.png)
Opencv-Python图像透视变换cv2.warpPerspective的示例
2023-06-27 19:03:18
![](https://img.aspxhome.com/file/2023/2/98842_0s.gif)