#slist = [[i for i in range(4)] for l in range(8)] # slist = [ # [4, 5, 2, 7], # [8, 7, 4, 2], # [1, 9, 7, 3], # [0, 4, 5, 6], # ] # 4*4 slist = [ [4, 5, 2, 7], [8, 7, 4, 2], [1, 9, 7, 3], [0, 4, 5, 6], [1, 9, 7, 3], ] # 4*5 # slist = [ # [1, 0, 1, 8, 4], # [9, 4, 9, 7, 5], # [7, 5, 7, 4, 2], # [3, 6, 3, 2, 7] # ] # 5*4 # slist = [ # [1, 0, 1, 8, 4], # [9, 4, 9, 7, 5], # [7, 5, 7, 4, 2], # [3, 6, 3, 2, 7], # [7, 5, 7, 4, 2], # ] # 5*5 # 旋轉90° def rotate_90(lists): row = len(lists) # 行 column = len(lists[0]) # 列 if row == column: # 行列相等 olist = [[i for i in range(column)] for l in range(row)] # 生成新列表 else: # 行列不等 olist = [[i for i in range(row)] for l in range(column)] # 生成新列表 for i in range(row): for l in range(column): #print('%d%d換%d%d' % (i, l, l, abs(i - row + 1))) olist[l][abs(i - row + 1)] = lists[i][l] return olist olist = rotate_90(slist) print('正常數組:%r\n轉90°後數組:%r' % (slist, olist)) # 分解步驟以下,數字等於二維數組索引 # 原數組 4行4列 新數組 # [4, 5, 2, 7]00到03,01到13,02到23,03到33[0, 1, 8, 4] # [8, 7, 4, 2]10到02,11到12,12到22,13到32[4, 9, 7, 5] # [1, 9, 7, 3]20到01,21到11,22到21,23到31[5, 7, 4, 2] # [0, 4, 5, 6]30到00,31到10,32到20,33到30[6, 3, 2, 7] # 原數組 5行4列 新數組 # [4, 5, 2, 7]00到04,01到14,02到24,03到34[1, 0, 1, 8, 4] # [8, 7, 4, 2]10到03,11到13,12到23,13到33[9, 4, 9, 7, 5] # [1, 9, 7, 3]20到02,21到12,22到22,23到32[7, 5, 7, 4, 2] # [0, 4, 5, 6]30到01,31到11,32到21,33到31[3, 6, 3, 2, 7] # [1, 9, 7, 3]40到00,41到10,42到20,43到30 # 原數組 4行5列 新數組 # [1, 0, 1, 8, 4]00到03,01到13,02到23,03到33,04到43[3, 7, 9, 1] # [9, 4, 9, 7, 5]10到02,11到12,12到22,13到32,14到42[6, 5, 4, 0] # [7, 5, 7, 4, 2]20到01,21到11,22到21,23到31,24到41[3, 7, 9, 1] # [3, 6, 3, 2, 7]30到00,31到10,32到20,33到30,34到40[2, 4, 7, 8] # [7, 2, 5, 4] # 旋轉180度 def rotate_180(lists): row = len(lists) # 行 column = len(lists[0]) # 列 olist = [[i for i in range(column)] for l in range(row)] # 生成新列表 remainder = row % 2 for i in range(row // 2 + remainder): for l in range(column): # 先判斷奇偶行--是奇數行,再判斷是否在操做中間行--列表是奇數行,當前也在中間行,超過中間列就中止 # 由於偶數中間行是上下行對角互換,因此要按列依次互換 # 奇數中間行是在中間行左右互換,因此超過中間列就再也不繼續互換 if remainder == 1 and i == (row - 1) // 2 and l > (column - 1) // 2: break # 原位置,互換位置依次取值 olist[abs(i - row + 1)][abs(l - column + 1)] = lists[i][l] olist[i][l] = lists[abs(i - row + 1)][abs(l - column + 1)] return olist olist = rotate_180(slist) print('正常數組:%r\n轉180°後數組:%r' % (slist, olist)) # 分解步驟以下,數字等於二維數組索引 # 原數組 偶數行 新數組 # [4, 5, 2, 7]00換33,01換32,02換31,03換30[6, 5, 4, 0] # [8, 7, 4, 2]10換23,11換22,12換21,13換20[3, 7, 9, 1] # [1, 9, 7, 3] [2, 4, 7, 8] # [0, 4, 5, 6] [7, 2, 5, 4] # 原數組 奇數行 新數組 # [4, 5, 2, 7]00換43,01換42,02換41,03換40[3, 7, 9, 1] # [8, 7, 4, 2]10換33,11換32,12換31,13換30[6, 5, 4, 0] # [1, 9, 7, 3]20換23,21換22 [3, 7, 9, 1] # [0, 4, 5, 6] [2, 4, 7, 8] # [1, 9, 7, 3] [7, 2, 5, 4] # 旋轉270度 def rotate_270(lists): row = len(lists) column = len(lists[0]) if row == column: # 行列相等 olist = [[i for i in range(column)] for l in range(row)] # 生成新列表 else: olist = [[i for i in range(row)] for l in range(column)] # 生成新列表 for i in range(row): for l in range(column): # print('%d%d換%d%d' % (i, l, abs(l - column + 1), i)) olist[abs(l - column + 1)][i] = lists[i][l] return olist olist = rotate_270(slist) print('正常數組:%r\n轉270°後數組:%r' % (slist, olist)) # 分解步驟以下,數字等於二維數組索引 # 原數組 4行4列 新數組 # [4, 5, 2, 7]00到30,01到20,02到10,03到00[7, 2, 3, 6] # [8, 7, 4, 2]10到31,11到21,12到11,13到01[2 ,4, 7, 5] # [1, 9, 7, 3]20到32,21到22,22到12,23到02[5, 7, 9, 4] # [0, 4, 5, 6]30到33,31到23,32到13,33到03[4, 8, 1, 0] # 原數組 5行4列 新數組 # [4, 5, 2, 7]00到30,01到20,02到10,03到00[7, 2, 3, 6, 3] # [8, 7, 4, 2]10到31,11到21,12到11,13到01[2, 4, 7, 5, 7] # [1, 9, 7, 3]20到32,21到22,22到12,23到02[5, 7, 9, 4, 9] # [0, 4, 5, 6]30到33,31到23,32到13,33到03[4, 8, 1, 0, 1] # [1, 9, 7, 3]40到34,41到24,42到14,43到04 # 原數組 4行5列 新數組 # [1, 0, 1, 8, 4]00到40,01到30,02到20,03到10,04到00[4, 5, 2, 7] # [9, 4, 9, 7, 5]10到41,11到31,12到21,13到11,14到01[8, 7, 4, 2] # [7, 5, 7, 4, 2]20到42,21到32,22到22,23到12,24到02[1, 9, 7, 3] # [3, 6, 3, 2, 7]30到43,31到33,32到23,33到13,34到03[0, 4, 5, 6] # [1, 9, 7, 3] #正方形對角互換(左上到右下連線分紅右上和左下兩個區域互換) print('對角交換前數組:%r' % slist) for i in range(len(slist)): for l in range(len(slist[0])): if i < l: tint = slist[i][l] slist[i][l] = slist[l][i] slist[l][i] = tint print('對角交換後數組:%r' % slist) # 分解步驟以下,數字等於二維數組索引 # 原數組 新數組 # [4, 5, 2, 7]01換10,02換20,03換30[4, 8, 1, 0] # [8, 7, 4, 2]12換21,13換31 [5, 7, 9, 4] # [1, 9, 7, 3]23換32 [2, 4, 7, 5] # [0, 4, 5, 6] [7, 2, 3, 6]