Introduction to Python Programming (3) Use of arrays _python

Source: Internet
Author: User
Tags arrays

1, Python's array can be divided into three kinds of types:

(1) list of common linked list , initialization can be a specific way to add dynamic elements.
Definition method: arr = [element]

(2) Tuple a fixed array , once defined, the number of elements can no longer be changed.
Definition mode: arr = (Element)

(2) Dictionary dictionary type , that is, a hash array.
Definition mode: arr = {element K:v}

2, the following specific description of the use of these arrays and techniques:

(1) List chain list array

A, initialization when defined

Copy Code code as follows:
A = [1,2,[1,2,3]]

b, not initialized when defined

One-dimensional arrays:

Copy Code code as follows:
arr = []

Multidimensional Arrays:

Copy Code code as follows:
arr = [I for I in range (ten), 1,[]] #注意, I for in XX this must be placed in the first position, or first define I,

Such as:

Copy Code code as follows:
arr = [I for I in range (5), J for J in Range (5), []]

This is wrong.
Copy Code code as follows:
i = 0
j = 0
arr = [I for I in range (5), J for J in Range (5), []]

That's right

C,del Statements and: Usage

You can use Start:end to represent an interval in an array (i >= start and I < end)
Del deletes the specified element in the array
Such as:

Copy Code code as follows:
Del Arr[0]
Del arr[0, 2]
Newarr = arr[0, 2]

D, traversing the array :

Copy Code code as follows:
For K, v. in Enumerate (ARR):
Print K, V

E, add elements :

One-dimensional

Copy Code code as follows:
Arr.append (' AAA ')

Two-dimensional
Copy Code code as follows:
Arr[0].append (' AAA ')

If you want to insert with Arr.insert (n, value) at any location
In addition, there is a special use:
arr = = [array element]
If you do not specify a subscript, you are allowed to increase the array element with + =

(2) Tuple fixed Array

Tuple is an immutable list, and once a Tuple is created, it cannot be changed in any way.
Here's a concrete example to illustrate:

Copy Code code as follows:
>>> t = ("A", "B", "C", "D", "E") #[1] are surrounded by parentheses to define
>>> T
(' A ', ' B ', ' C ', ' d ', ' e ')
>>> T[0] #[2] directly lists the elements of a subscript
A
>>> t[-1] #[3] Negative numbers indicate that the index minus 1 is the penultimate, and 0 is the first.
' Example '
>>> T[1:3] #[4] Here 1:3 is the range of i>=1 and i<3
(' B ', ' Mpilgrim ')

tuple does not have the method :

[1] can not add elements to the tuple, no append, Extend, insert and other methods.
[2] You cannot delete elements from tuple, no remove or pop methods.
[3] can not find elements in the tuple, there is no index method (index is lookup rather than index, index directly with subscript can, such as: T[0]).

benefits of using tuple :

* Tuple is faster than list operation. If you define a constant set of values, and the only thing to do with it is to iterate over it, use tuple instead of the list.
* If you do not need to modify the data to write protection, you can make the code more secure. Using tuple instead of a list is like having an implied assert statement that the data is constant. If you have to change these values, you need to perform a tuple to list conversion (you need to use a special function).
* Remember when I said dictionary keys can be strings, integers, and "Several other types"? Tuples is one of these types. Tuples can be used as a key in dictionary, but the list is not. In fact, things are more complicated than that. The Dictionary key must be immutable. The Tuple itself is immutable, but if you have a list of Tuple, it is considered mutable, and being used as dictionary key is unsafe. Only strings, integers, or other tuple that are dictionary safe can be used as dictionary key.

tuple can be converted to a list and vice versa .

The conversion mode is:
t = List (t)
Instead:
arr = tuple (arr)

(2) array of Dictionary (hash array) dictionaries

Copy Code code as follows:
The use of #Dictionary is simpler, it can store arbitrary values, and allows for different types of values, as described in the following example:
#下面例子中 A is an integer, B is a string, and C is an array, an example that fully illustrates the applicability of a hash array.
Dict_arr = {' A ': +, ' B ': ' Boy ', ' C ': [' o ', ' P ', ' Q ']}

#可以直接增加一个元素, the value of the element of the original key is changed if it has the same name
dict_arr[' d '] = ' dog '

#输出所有的key
Print Dict_arr.keys ()

#输出所有的value
Print dict_arr.values ()

#遍历数组
Import types
For K in Dict_arr:
v = dict_arr.get (k)
If Type (v) is types. ListType: #如果数据是list类型, continue traversing
Print K, '---'
For KK, VV in Enumerate (v):
Print KK, vv
print '---'
Else
Print Dict_arr.get (k)

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.