"Python" Rotates the 4*4 array by a new 90-degree array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.