Introduction to the use of of 3 arrays in Python programming

  • 2020-04-02 13:46:48
  • OfStack

1. Python arrays can be divided into three types:

(1) A common linked list , you can add elements dynamically through a specific method after initialization.
Definition: arr = [element]

(2) Tuples are fixed arrays , once defined, the number of elements cannot be changed.
Definition: arr = (element)

(2) Dictionary type , is the Hash array.
Arr = {element k:v}

2. The following details the use methods and techniques of these arrays:

(1) list array

A. Initialization at definition

a = [1,2,[1,2,3]]

B. Not initialized when defined

One-dimensional array:

arr = []

Multidimensional array:

arr = [i for i in range(10), 1,[]] # Pay attention to,  i for in xx  This has to be in the first place, otherwise you have to define it first i . 

Such as:

arr = [i for i in range(5), j for j in range(5), []]

This is wrong
i = 0
j = 0
arr = [i for i in range(5), j for j in range(5), []]

This is correct

C, Del statement and the use of:

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

del arr[0]
        del arr[0, 2]
       newarr = arr[0, 2]

D, Through the array :

for k, v in enumerate(arr):
        print k, v

E, Add elements :

A one-dimensional

arr.append('aaa')

A two-dimensional
arr[0].append('aaa')

If you want to insert at any place you want to use arr.insert(n, value)
Another special use is:
Arr += [array element]
Adding an array element with += is allowed without specifying subscripts .

(2) Tuples fix arrays

A Tuple is an immutable list, and once a Tuple is created, it cannot be changed in any way.
The following is a specific example:

>>> t = ("a", "b", "c", "d", "e")              #[1]  It's defined by enclosing it in parentheses 
>>> t
('a', 'b', 'c', 'd', 'e')
>>> t[0]                                       #[2]  Directly lists the elements of a subscript 
'a'
>>> t[-1]                                      #[3]  A negative number is the index that is inverted from behind  -1  Is the reciprocal of the first,  0 It's the first in order 
'example'
>>> t[1:3]                                     #[4]  Here,  1:3  is  i>=1 and i<3  The range of 
('b', 'mpilgrim')

A Tuple doesn't have a method :

[1] you can't add elements to a tuple, there's no append, extend, insert   Methods.
[2] you cannot remove an element from a tuple, and there are no remove or pop methods.
[3] you cannot find an element in a tuple, and there is no index method (index is a search, not an index, and the index is simply a subscript, such as t[0]).

The benefits of using a tuple :

* tuples operate faster than lists. If you define a constant set of values, and the only thing you need to do with it is iterate over it, use a tuple instead of a list.
* you can make your code more secure if you "write protect" data that doesn't need to be modified. Using a tuple instead of a list is like having an implicit assert statement that this data is constant. If you have to change these values, you need to perform a tuple to list transformation (using a special function).
* remember when I said dictionary keys can be strings, integers, and "several other types"? Tuples are one of these types. Tuples can be used as keys in dictionaries, but not lists. Actually, it's more complicated than that. A Dictionary key must be immutable. The Tuple itself is immutable, but if you have a Tuple of a list, it's considered mutable, and it's not safe to use as a dictionary key. Only strings, integers, or other dictionary-safe tuples can be used as dictionary keys.

A Tuple can be converted to a list, and vice versa .

The conversion mode is:
T is equal to list of t.
On the other hand:
Arr is equal to tuple of arr.

(2) Dictionary array

#Dictionary  , it can store any value, and allow different types of values, the following example to illustrate: 
# In the following example  a  Is an integer,  b  Is the string , c  It's an array, and this is a good example of the applicability of hash arrays. 
dict_arr = {'a': 100, 'b':'boy', 'c':['o', 'p', 'q']}
# You can add an element directly, and if it has the same name, it will change key The value of the element 
dict_arr['d'] = 'dog'
# Output all key
print dict_arr.keys()
# Output all value
print dict_arr.values()
# Through the array 
import types
for k in dict_arr:
    v = dict_arr.get(k)
    if type(v) is types.ListType: # If the data is list Type, continue the traversal 
        print k, '---'
        for kk, vv in enumerate(v):
            print kk, vv
        print '---'
    else:
        print dict_arr.get(k)


Related articles: