This article introduces the Python list. In computer languages, the Python list is a widely used language. If you have some practical skills related to the Python list, you can browse our articles. The following is a brief introduction to the articles.
Python does not have an array data structure, but the list is similar to an array, for example:
- A = [, 2] Then a [0] = 0, a [1] = 1, a [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
- a = [0 for x in range(0, 1000)]
For the Python list, the following is the definition of a two-dimensional array: directly defining a = [[], []. Here we define a 2*2, A two-dimensional array with an initial value of 0. Indirect Definition
- a = [0 for x in range(0, 1000)]
A two-dimensional array with 10x10 initially 0 is defined here. Later, I found a simpler method to define a two-dimensional array on the Internet: B = [[0] * 10] * 10, and define a two-dimensional array whose 10*10 is initially 0. And
- a=[[0 for x in range(10)] for y in range(10)]
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, it is estimated that the definition of array c is a multiplication of the value type, while the previous B is a multiplication of the type, because the one-dimensional array is a reference to borrow the value type and reference type in C, ). The above article introduces the Python list.