Introduction to array and multi-dimensional array usage in python, multi-dimensional python

Source: Internet
Author: User

Introduction to array and multi-dimensional array usage in python, multi-dimensional python

You can add a. append ('A. As long as it is added in order, there is no problem.
You can use the subscript:

The Code is as follows:
A [0]
A [1]

If the subscript does not exist, an exception is thrown. In this case, you need to add the element first and then reference it. If you want to retain the space in advance, you can use a loop to give the list, each element has a default value, and then reference it will not be a problem.
For example:

The Code is as follows:
A = []
For I in range (100 ):
A. append ([])
For j in range (100 ):
A [I]. append (0)

In this way, an array with the default value of 100*100 as 0 is generated.


# Create an array with a width of 3 and a height of 4
# [0, 0, 0],
# [0, 0],
# [0, 0],
# [0, 0]
MyList = [[0] * 3] * 4 but when myList [0] [1] = 1, it is found that the entire second column is assigned a value and becomes

[[0, 1, 0],

[0, 1],

[0, 1],

[0, 1]

Why? I don't understand it for a moment. I will refer to The Python Standard Library to find The answer later.

List * n-> n shallow copies of list concatenated

Example:

The Code is as follows:
>>> Lists = [[] * 3
>>> Lists
[[], [], []
>>> Lists [0]. append (3)
>>> Lists
[[3], [3], [3] [[]

Is a list containing an empty list element, so [[] * 3 indicates three references pointing to this empty list element, modify any

An element changes the entire list:

Therefore, you need to create a multi-dimensional array in another way to avoid copying:

The Code is 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

The two-dimensional array creation method before [[3], [5], [7] is as follows:

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

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

The Code is as follows:
Int ** pTwoDimArr = new int [10] [20];
PTwoDimArr [0] [0] = 5;

This seems intuitive. However, compilation may fail:

The Code is as follows:
Cannot convert from 'int (*) [20] 'to 'int **'


If so:

The Code is as follows:
Int pTwoDimArr [10] [20];
PTwoDimArr [0] [0] = 5;

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

The Code is as follows:
Void main (){
Int pTwoDimArr [1000] [1000];
}

Compilation is okay, and the running will die.

You can use it as a global variable, but it is best to stop it.

The Code is as follows:
Int pTwoDimArr [1000] [1000];
Void main (){
}


In this way:

The Code is as follows:
Int (* pTwoDimArr) [1000] = new int [1000] [1000];
PTwoDimArr [0] [0] = 5;

Dynamic Allocation is acceptable. First, remember to release the memory, and second, the dimension should be fixed (the first dimension can be changed). Isn't it uncomfortable?

Because C ++ is a "static" language, everything needs to be determined during the compilation period. Therefore, in the definition of multi-dimensional arrays, except the first dimension can be changed, other dimensions must be fixed as constants, because arrays are stored linearly in the memory.
From: http://www.111cn.net/phper/python/41780.htm


C # What is the difference between a multi-dimensional array and an array? Under what different circumstances?

Int [,] a; // [] contains a multi-dimensional array with commas.
Int [] [] [] a; // multiple [] [] [] [] square brackets are array Arrays

Multi-dimensional array is a continuous memory, suitable for fixed-size memory operations, such as 2D drawing and images, graph algorithms, fonts, etc.
The advantage is fast read/write speed, and the disadvantage is fixed.
Array array, discontinuous multi-block memory, used to implement some dynamic data structures, such as dynamic arrays, storage of data with changing quantities, etc.
Advantages: you can determine the size as needed, saving space, disadvantages, dispersion, and performance is a little slow.

In fact, only C/C ++ C # provides multi-dimensional arrays for further differentiation. Many mainstream languages, such as java, php, and python, do not provide multi-dimensional arrays but arrays. The performance and usage differences are very small. It is not a read-write intensive, resource-exhausted application, it is difficult to really touch on the performance difference.
 
How can two-dimensional arrays in python perform addition, subtraction, multiplication, and Division operations without loops?

>>> A = [[1, 2, 3, 6], [2, 6, 3, 9], [3, 7, 9, 0]
>>> B = [[,], [,], [,]
>>> C = [[a [I] [j]-B [I] [j] for j in range (4)] for I in range (3)]
>>> C
[[-4,-5, 0, 5], [-3,-1, 1, 5], [-9,-27,-47,-98]

Related Article

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.