Python_二維數組
- 2020 年 1 月 19 日
- 筆記
例1:將數組旋轉90度
1 a = [[i for i in range(4)] for n in range(4)] 2 print(a) 3 # 遍歷大序列 4 for a_index, w in enumerate(a): # enumaerate()遍曆數據對象,同時列出數據和數據下標 5 # 遍歷大序列里的小序列 6 for m_index in range(a_index, len(w)): # range(w_index, )使for循環從w_index行開始替換 7 tmp = a[m_index][a_index] # 將大序列里的值存起來 8 a[m_index][a_index] = w[a_index] # 將小序列的值放到大序列 9 a[a_index][m_index] = tmp #將存起來的值放到小序列里 10 print('----------------') 11 for r in a: #查看轉換過程 12 print(r)
rotating
結果:
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]] ---------------- [0, 0, 0, 0] [0, 1, 2, 3] [0, 1, 2, 3] [0, 1, 2, 3] ---------------- [0, 0, 0, 0] [0, 1, 1, 1] [0, 1, 2, 3] [0, 1, 2, 3] ---------------- [0, 0, 0, 0] [0, 1, 1, 1] [0, 1, 2, 2] [0, 1, 2, 3] ---------------- [0, 0, 0, 0] [0, 1, 1, 1] [0, 1, 2, 2] [0, 1, 2, 3]
View Code