Add A.append (' a ') on it. As long as the order is added, there is no problem.
When used, the subscript can be used completely:
The code is as follows |
Copy Code |
A[0] A[1]
|
An exception is thrown if the result references a subscript that does not exist. At this point, you need to add elements first, then the reference will be no problem. If you want to reserve space in advance, you can use loops to give the list, a default value for each element, and then reference will not be a problem.
Such as:
The code is as follows |
Copy Code |
A=[] For I in range (100): A.append ([]) For j in Range (100): A[i].append (0)
|
This generates an array with a 100*100 default value of 0.
#创建一个宽度为3, array with a height of 4
#[[0,0,0],
# [0,0,0],
# [0,0,0],
# [0,0,0]]
myList = [[0] * 3] * 4 But when the operation mylist[0][1] = 1 o'clock, the whole second column is found to be assigned, and becomes
[[0,1,0],
[0,1,0],
[0,1,0],
[0,1,0]]
Why... I don't understand, back to the Python Standard Library to find the answer
List * N->n shallow copies of list concatenated, the connection of a shallow copy of n list
Cases:
The code is as follows |
Copy Code |
>>> lists = [[]] * 3 >>> lists [[], [], []] >>> Lists[0].append (3) >>> lists [[3], [3], [3]] [[]] |
is a list containing an empty list element, so [[]]*3 represents 3 references to this empty list element, modifying any
An element changes the entire list:
So you need to create multidimensional arrays in a different way to avoid shallow copies:
The code is as follows |
Copy Code |
>>> lists = [[] for I in range (3)] >>> Lists[0].append (3) >>> Lists[1].append (5) >>> Lists[2].append (7) >>> lists
|
[[3], [5], [7]] The two-dimensional array was created in the following way:
MyList = [([0] * 3) for I in range (4)]
For example, to dynamically create a two-dimensional array:
The code is as follows |
Copy Code |
int** Ptwodimarr = new INT[10][20]; Ptwodimarr[0][0] = 5; |
This seems intuitive, but the compilation can be an error:
The code is as follows |
Copy Code |
Cannot convert from ' int (*) [~] ' to ' int * * * |
If so:
The code is as follows |
Copy Code |
int ptwodimarr[10][20]; Ptwodimarr[0][0] = 5; |
is OK, but the memory of this array is allocated on the stack, so
The code is as follows |
Copy Code |
void Main () { int ptwodimarr[1000][1000]; }
|
No problem compiling, running will die
It's okay to be a global variable, but it's best to stop it.
The code is as follows |
Copy Code |
int ptwodimarr[1000][1000]; void Main () { } |
Such
The code is as follows |
Copy Code |
int (*ptwodimarr) [1000] = new int[1000][1000]; Ptwodimarr[0][0] = 5; |
Dynamic allocation is possible, one can remember to release memory, second dimension or fixed (first dimension variable), is not a bit uncomfortable
Because C + + is a "static" language, in the compile period to determine everything, so in the definition of multidimensional arrays in addition to the first dimension variable, the other several dimensions are fixed to constant, because the array in memory is linear storage.