python3--算法基础:二维数组转90度
python3--算法基础:二维数组转90度
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
二维数组转90度
[0, 0, 0, 0]
[1, 1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
#!/usr/bin/env python # -*- coding:utf-8 -*- # 算法基础:二维数组转90度 a = [col for col in range(4)] print(a) print("--------------------") a = [[col for col in range(4)] for row in range(4)] print(a) for li in a : print(li) print("--------------------") for i, row in enumerate(a): print(i, row) for j in range(len(row) - i): tmp = a[j + i][i] a[j + i][i] = row[j + i] a[i][j + i] = tmp print("*****************") for r in a:print(r) print(a) for rr in a:print(rr) print("=-----------改range开始值----------") a = [col for col in range(4)] print(a) print("--------------------") a = [[col for col in range(4)] for row in range(4)] print(a) for li in a : print(li) print("--------------------") for i, row in enumerate(a): print(i, row) for j in range(i, len(row)): # 改range开始值 print(j) tmp = a[j][i] a[j][i] = row[j] a[i][j] = tmp print("*****************") for r in a:print(r) print(a) for rr in a:print(rr)
以上代码运行结果:
D:\ProgramData\Anaconda3\python.exe D:/py/day001/day1/test.py [0, 1, 2, 3] -------------------- [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]] [0, 1, 2, 3] [0, 1, 2, 3] [0, 1, 2, 3] [0, 1, 2, 3] -------------------- 0 [0, 1, 2, 3] ***************** [0, 0, 0, 0] [1, 1, 2, 3] [2, 1, 2, 3] [3, 1, 2, 3] 1 [1, 1, 2, 3] ***************** [0, 0, 0, 0] [1, 1, 1, 1] [2, 2, 2, 3] [3, 3, 2, 3] 2 [2, 2, 2, 3] ***************** [0, 0, 0, 0] [1, 1, 1, 1] [2, 2, 2, 2] [3, 3, 3, 3] 3 [3, 3, 3, 3] ***************** [0, 0, 0, 0] [1, 1, 1, 1] [2, 2, 2, 2] [3, 3, 3, 3] [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]] [0, 0, 0, 0] [1, 1, 1, 1] [2, 2, 2, 2] [3, 3, 3, 3] =-----------改range开始值---------- [0, 1, 2, 3] -------------------- [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]] [0, 1, 2, 3] [0, 1, 2, 3] [0, 1, 2, 3] [0, 1, 2, 3] -------------------- 0 [0, 1, 2, 3] 0 1 2 3 ***************** [0, 0, 0, 0] [1, 1, 2, 3] [2, 1, 2, 3] [3, 1, 2, 3] 1 [1, 1, 2, 3] 1 2 3 ***************** [0, 0, 0, 0] [1, 1, 1, 1] [2, 2, 2, 3] [3, 3, 2, 3] 2 [2, 2, 2, 3] 2 3 ***************** [0, 0, 0, 0] [1, 1, 1, 1] [2, 2, 2, 2] [3, 3, 3, 3] 3 [3, 3, 3, 3] 3 ***************** [0, 0, 0, 0] [1, 1, 1, 1] [2, 2, 2, 2] [3, 3, 3, 3] [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]] [0, 0, 0, 0] [1, 1, 1, 1] [2, 2, 2, 2] [3, 3, 3, 3] Process finished with exit code 0