# coding:utf8 __author__ = 'bluesli' ''' [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] 实际上就是沿着对角线交换数字:但是已经交换过了的就不能再交换,所以必须让内层循环从i开始; ''' def rotation(two_demensional_array=[]): for i in range(len(two_demensional_array)): for j in range(i,len(two_demensional_array)): temp = two_demensional_array[i][j] two_demensional_array[i][j] = two_demensional_array[j][i] two_demensional_array[j][i] = temp def show(two_demensional_array=[]): for data in two_demensional_array: print(data) if __name__ == '__main__': datas = [[col for col in range(4)] for row in range(4)] show(datas) rotation(datas) show(datas) ''' enumerate() 函数用于将一个可遍历的数据对象 (如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标 ,一般用在 for 循环当中。 ''' # for r_index,element in enumerate(datas): # print(r_index,element)