Operation Scheme of the data structure actually used in the Python Array

Source: Internet
Author: User
Tags python list

This article mainly introduces the operation scheme of the Python array in the actual application process. If you are confused about the actual application process of the Python array, you can browse the following articles, I hope it will be helpful to you. The following is a detailed description of the article.

This article does not detail the Python list. You can refer to the Python documentation. The Python array does not have an array data structure, but the list is similar to an array, for example:

 
 
  1. a=[0,1,2] 

Then

 
 
  1. a[0]=0, a[1]=1, a[[2]=2  
  2.  

However, a problem is raised, that is, if array a is to be defined as 0 to 999, then it may be implemented through a = range (0, 1000. Or omitted as a = range (1000). If you want to define a with a length of 1000 and the initial value is 0

 
 
  1. a = [0 for x in range(0, 1000)]  

The following is the definition of a two-dimensional array: direct definition

 
 
  1. a=[[1,1],[1,1]],  

A two-dimensional array of 2*2 with an initial value of 0 is defined here. Indirect Definition

 
 
  1. a=[[0 for x in range(10)] for y in range(10)]  

Here, the Python array defines a two-dimensional array with the initial value of 10x10 as 0. Later, I found a simpler method to define two-dimensional arrays on the Internet:

 
 
  1. b = [[0]*10]*10 

Defines a two-dimensional array with 10x10 initially 0. And

 
 
  1. a=[[0 for x in range(10)] for y in range(10)]  
  2.  

Comparison: the result of print a = B is True. However, after B's definition method is used to replace a, the previous programs that can run normally also failed. After careful analysis, the difference is obtained: When a [0] [0] = 1, only a [0] [0] is 1, and all others are 0. When B [0] [0] = 1, a [0] [0], a [1] [0], and only a [9, 0] is 1. Therefore, the 10 small one-dimensional data in the large array is all referenced in the same way, that is, pointing to the same address. Therefore, B = [[0] * 10] * 10 does not conform to the two-dimensional array in our general sense.

At the same time, the definition of c = [0] * 10 has the same effect as that of c = [0 for x in range (10, the Python array does not have the same reference above. It is estimated that the definition of array c is a multiplication of the value type, while that of array B is a multiplication of the type, because the one-dimensional array is a reference.

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.