This article mainly introduces an array of n*n, if the rotation of 90 degrees
First, it is simple to define a one-dimensional array, as follows:
A = [I for I in Range]]print (a)-----results-----0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
This notation is the same as the following:
A = []for i in range: a.append (i) print (a)-----results-----[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
So how should the two-dimensional arrays be created, as follows:
A = [[col for Col in Range (4)] for row in range (4)] #创建一个4 Two-dimensional array print (a)-----results----[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1 , 2, 3], [0, 1, 2, 3]]
If you want to visualize something, tweak it slightly:
A = [[col for Col in Range (4)] for row in range (4)]for I in A: print (i)-----results-----[0, 1, 2, 3][0, 1, 2, 3][0, 1, 2, 3 ][0, 1, 2, 3]
Next we're going to rotate the array 90 degrees to
[0, 1, 2, 3] [0, 1, 2, 3] [0, 1, 2, 3] [0, 1, 2, 3]
Convert to:
[0, 0, 0, 0] [1, 1, 1, 1] [2, 2, 2, 2] [3, 3, 3, 3]
To implement this function, we first split the step into 3 steps:
The first step:
Step Two:
Step Three:
The code is as follows:
data = [[col for Col in Range (4)] for row in range (4)]for Row_index, row in Enumerate (data): for Col_index in range (RO W_index, Len (Row)): tmp = Data[col_index][row_index] #设置一个临时变量 Data[col_index][row_index] = Row[col_ Index] data[row_index][col_index] = tmp print (') #防止打印结果看上去混乱, enter an empty content for R in data: # Steps to print out the conversion results print (r)-----Results-----[0, 0, 0, 0] [1, 1, 2, 3][2, 1, 2, 3][3, 1, 2, 3][0, 0, 0, 0][1, 1, 1,, 2, 2, 3][3, 3, 2, 3][0, 0, 0, 0][1, 1, 1,, 2, 2, 2][3, 3, 3, 3][0, 0, 0, 0][1, 1, 1,, 2, 2, 2][3, 3, 3, 3]
python-two-dimensional array for 90 degree rotation