python-two-dimensional array for 90 degree rotation

Source: Internet
Author: User

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

Related Article

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.