Introduction to arrays and multidimensional array usages in Python

Source: Internet
Author: User
Tags shallow copy

Increase the a.append (' a ') on it. As long as the order is added, there is no problem.
When used, the subscript can be used completely:

Code to copy code as follows
A[0]
A[1]

However, an exception is thrown if the result refers to a non-existent subscript. At this point, you need to add the element first, then the reference will be no problem. If you want to reserve space beforehand, you can use a loop to give the list, each element a default value, then the reference will not have a problem.
Such as:

Code to copy code as follows
A=[]
For I in range (100):
A.append ([])
For j in Range (100):
A[i].append (0)

This generates an array with a default value of 0 for the 100*100.


#创建一个宽度为3, an array with a height of 4
#[[0,0,0],
# [0,0,0],
# [0,0,0],
# [0,0,0]]
myList = [[0] * 3] * 4 But when operation mylist[0][1] = 1 o'clock, the entire second column is found to be assigned, which becomes

[[0,1,0],

[0,1,0],

[0,1,0],

[0,1,0]]

Why... The Python standard Library found the answer in the back.

List * N->n shallow copies of list concatenated, n a shallow copy of list of connections

Cases:

Code to copy code as follows
>>> lists = [[]] * 3
>>> lists
[[], [], []]
>>> Lists[0].append (3)
>>> lists
[3], [3], [3]] [[]]

is a list that contains an empty list element, so [[]]*3 represents 3 references to this empty list element and modifies any

An element will change the entire list:

So you need to create a multidimensional array in a different way to avoid shallow copying:

Code to copy code as follows
>>> lists = [[] for I in RA (www.111cn.net) nge (3)]
>>> Lists[0].append (3)
>>> Lists[1].append (5)
>>> Lists[2].append (7)
>>> lists

[[3], [5], [7]] before the two-dimensional array is created as follows:

MyList = [([0] * 3) for I in range (4)]

For example, to dynamically create a two-dimensional array:

Code to copy code as follows
int** Ptwodimarr = new INT[10][20];
Ptwodimarr[0][0] = 5;

This may seem straightforward, but the compilation will go wrong:

Code to copy code as follows
Cannot convert from ' int (*) [+] ' to ' int * * '


If so:

Code to copy code as follows
int ptwodimarr[10][20];
Ptwodimarr[0][0] = 5;

Yes, but the memory of this array is allocated on the stack, so,

Code to copy code as follows
void Main () {
int ptwodimarr[1000][1000];
}

Compile no problem, the operation will be dead

It's OK as a global variable, but it's better to stop it.

Code to copy code as follows
int ptwodimarr[1000][1000];
void Main () {
}


Such

Code to copy code as follows
int (*ptwodimarr) [+] = new int[1000][1000];
Ptwodimarr[0][0] = 5;

Dynamic allocation is possible, to remember to release memory, and the number of dimensions or to be fixed (the first dimensional variable), is not a bit uncomfortable

Because C + + is a "static" language, in the compile time 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.
From:http://www.111cn.net/phper/python/41780.htm

Introduction to arrays and multidimensional array usages in Python

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.