The rotation of a two-dimensional array is actually the case of swapping elements inside the array; here is a 4x4 two-dimensional array, [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]], and now requires that the two-dimensional array be converted to the following form, [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]. Here's a look at the flowchart:
Flow chart:
Flow chart as shown above, is to carry out a simple interchange, the following we use code to achieve the row and column interchange:
data = [[i forIinchRange4)] forJinchRange4]print (data) #定义行的初始值, we found that the row changed from 0 onwards to 3col=0 whileCol <4: #循环的结束条件, because there are only four rows, so loop 4 is the end forRowinchRange (col,4): #这里我们让行列的序号每次循环递增1, avoid converting the TEM again before conversion=Data[row][col] #存储临时变量, lest the value change after replacement Data[row][col]=Data[col][row] #列表行列的值进行互换 Data[col][row]=tem Col+=1#列的索引每次加一print (data)
The concept of the above code is the idea of the flowchart, only need to make the necessary conversion, pay attention to the conversion process so that the value of the change. So the flowchart is still very important.
Run the code as follows:
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]
Day4 two-dimensional array rotation 90 degrees