1, the Python array can be divided into three types:
(1) list ordinary linked list , after initialization can be dynamically added to the element through a specific method.
How to define: arr = [element]
(2) Tuple fixed array , once defined, its number of elements can no longer change.
How to define: Arr = (Element)
(2) Dictionary dictionary type , which is a hash array.
How to define: Arr = {element K:v}
2, the following details the use of these array methods and techniques:
(1) Array of list lists
A. Initialize when defining
Copy the Code code as follows:
A = [1,2,[1,2,3]]
B. Do not initialize when defining
One-dimensional arrays:
Copy the Code code as follows:
arr = []
Multidimensional Arrays:
Copy the Code code as follows:
arr = [I for I in Range], 1,[]] #注意, I-in XX must be placed in the first position, otherwise I should be defined first,
Such as:
Copy the Code code as follows:
arr = [I for I in a range (5), J for J in Range (5), []]
This is wrong.
Copy CodeThe code is as follows:
i = 0
j = 0
arr = [I for I in a 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
For example: copy code code as follows:
Del Arr[0]
Del arr[0, 2]
Newarr = arr[0, 2]
D, iterate through the array :
Copy the Code code as follows:
For K, V in Enumerate (arr):
Print K, V
E, add elements :
One-dimensional
Copy the Code code as follows:
Arr.append (' AAA ')
Two-dimensional
Copy CodeThe code is 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 usage:
arr + = [array elements]
in cases where subscript is not specified, it is allowed to increment the array element with + =。
(2) fixed array of Tuple
A tuple is an immutable list, and once a tuple is created, it cannot be changed in any way.
Here's a concrete example:
Copy the 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 number, from the back of the index 1 is the penultimate, 0 is the first of the CIS
' Example '
>>> T[1:3] #[4] Here 1:3 is the interval of i>=1 and i<3
(' B ', ' Mpilgrim ')
a method that the tuple does not have :
[1] can not add elements to a tuple, no append, Extend, insert and other methods.
[2] Cannot delete element from tuple, no remove or pop method.
[3] Cannot find elements in a tuple, there is no index method (index is a lookup instead of an index, indexes are directly subscript, 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 you want to do with it is to iterate over it, use tuple instead of list.
* You can make your code more secure if you write-protect data that does not need to be modified. Using a tuple instead of a list is like having an implied Assert statement, which indicates that the data is a 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 key in dictionary, but 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 it is unsafe to be used as a dictionary key. Only strings, integers, or other dictionary-safe tuples can be used as dictionary keys.
a tuple can be converted into a list and vice versa .
The conversion mode is:
t = List (t)
Instead:
arr = tuple (arr)
(2) Dictionary (hash array) dictionary array
Copy the Code code as follows:
The use of #Dictionary is simple, 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, and this example fully illustrates the applicability of the hash array.
Dict_arr = {' A ': ', ' B ': ' Boy ', ' C ': [' o ', ' P ', ' Q ']}
#可以直接增加一个元素, if it has the same name, it changes the value of the element of the original key
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)