Day4 two-dimensional array rotation 90 degrees

Source: Internet
Author: User

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

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.