Python_二维数组
例1:将数组旋转90度
a = [[i for i in range(4,8)] for n in range(4,8)] print(a) # 遍历大序列 for a_index, w in enumerate(a): # enumaerate()遍历数据对象,同时列出数据和数据下标 # 遍历大序列里的小序列 for w_index in range(a_index, len(w)): # range(w_index, )使for循环从w_index行开始替换 tmp = a[w_index][a_index] # 将大序列里的值存起来 a[w_index][a_index] = w[w_index] # 将小序列的值放到大序列 a[a_index][w_index] = tmp #将存起来的值放到小序列里 print('----------------') for r in a: #查看转换过程 print(r)
结果:
[[4, 5, 6, 7], [4, 5, 6, 7], [4, 5, 6, 7], [4, 5, 6, 7]] ---------------- [4, 4, 4, 4] [5, 5, 6, 7] [6, 5, 6, 7] [7, 5, 6, 7] ---------------- [4, 4, 4, 4] [5, 5, 5, 5] [6, 6, 6, 7] [7, 7, 6, 7] ---------------- [4, 4, 4, 4] [5, 5, 5, 5] [6, 6, 6, 6] [7, 7, 7, 7] ---------------- [4, 4, 4, 4] [5, 5, 5, 5] [6, 6, 6, 6] [7, 7, 7, 7]