Python_二维数组

例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