Requirement: Create a new 4*4 array by rotating an array of 4*4 90 degrees
The original 4*4 array:
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
4*4 array after 90 degrees of rotation:
[0, 0, 0, 0]
[1, 1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
Diagram Process:
The first step:
Step Two:
Step Three:
The code is as follows:
1 #Initializes an array of 4*42Array =[[col forColinchRange (4)] forRowinchRange (4)]3 #Print to see what the array looks like4 forRowincharray:5 Print(Row)6 #Cycle: The row index and the number of rows of the array are found through the enumerate function7 forR_index,rowinchEnumerate (array):8 #Small Loop9 forC_indexinchRange (R_index,len (row)):Ten #The value of the first column in each loop is saved to temp variable Onetemp =Array[c_index][r_index] A #Replace the value of the first row in each loop with the value of the first column in each loop -Array[c_index][r_index] =Array[r_index][c_index] - #Replace the value of the TEMP variable that you previously deposited with the first row in each loop theArray[r_index][c_index] =Temp - Print('-------------------') - forRinchArrayPrint(r)
A different way to achieve:
1 # Initializes an array of 4*4 2 for inch for in range (4)]3 for in range (len (array2)):4 For in range (4)] 5 Print (array_new)
"Python" Rotates the 4*4 array by a new 90-degree array