Introduction to Python Programming (3) Use of arrays-Python tutorial

Source: Internet
Author: User
This article mainly introduces how to use Python arrays. For more information, see 1. Python arrays can be divided into three types:

(1)List common linked listAfter initialization, you can dynamically add elements through specific methods.
Definition method: arr = [element]

(2)Tuple fixed arrayOnce defined, the number of elements cannot be changed.
Definition method: arr = (element)

(2)Dictionary typeThat is, the Hash array.
Definition method: arr = {element k: v}

2. The following describes how to use these arrays and their skills:

(1) list linked list array

A. initialization during Definition

The code is as follows:

A = [1, 2, 2, 3]

B. Non-initialization during Definition

One-dimensional array:

The code is as follows:

Arr = []

Multi-dimensional array:

The code is as follows:

Arr = [I for I in range (10), 1, [] # note that I for in xx must be placed in the first position; otherwise, you must first define I,

For example:

The code is as follows:

Arr = [I for I in range (5), j for j in range (5), []


This is wrong

The code is as follows:

I = 0
J = 0
Arr = [I for I in range (5), j for j in range (5), []


This is correct

C,Del statement and: usage

You can use start: end to represent a range (I> = start and I <end) in the array)
Del deletes the specified element in the array.
For example:

The code is as follows:

Del arr [0]
Del arr [0, 2]
Newarr = arr [0, 2]

D,Traverse arrays:

The code is as follows:

For k, v in enumerate (arr ):
Print k, v

E,Add element:

One-dimensional

The code is as follows:

Arr. append ('aaa ')


Two-dimensional

The code is as follows:

Arr [0]. append ('aaa ')


If you want to insert arr. insert (n, value) at any location)
In addition, there is a special usage:
Arr + = [array element]
If no value is specified, the array element can be added with + =..

(2)Tuple fixed array

Tuple is an immutable list. Once a tuple is created, it cannot be changed in any way.
The following is an example:

The code is as follows:

>>> T = ("a", "B", "c", "d", "e") # [1] surrounded by parentheses
>>> T
('A', 'B', 'C', 'D', 'E ')
>>> T [0] # [2] directly list the elements of a certain base object
'A'
>>> T [-1] # [3] indicates a negative number. from the last index,-1 indicates the last index, and 0 indicates the first index.
'Example'
>>> T [] # [4] here is the interval between I> = 1 and I <3
('B', 'mpilgrim ')

Tuple does not have a method:

[1] elements cannot be added to tuple. there are no append, extend, insert, and other methods.
[2] elements cannot be deleted from tuple. the remove or pop methods are not available.
[3] you cannot search for elements in tuple. there is no index method (index is a search instead of an index, and you can directly use a subscript for the index, for example, t [0]).

Benefits of using tuple:

* Tuple is faster than list operations. If you define a constant set of values, and the only thing you want to do with it is to continuously traverse it, use tuple instead of list.
* If you perform "write protection" on data that does not need to be modified, the code can be safer. Using tuple instead of list is like having an implicit assert statement, which indicates that the data is a constant. If you must change these values, you need to convert tuple to list (a special function is required ).
* Do you still remember that dictionary keys can be strings, integers, and "other types? Tuples is one of these types. Tuples can be used as the key in dictionary, but the list cannot. In fact, things are more complicated than that. The Dictionary key must be unchangeable. Tuple itself cannot be changed, but if you have a list of tuple, it is considered variable and it is not safe to use it as a dictionary key. Only string, integer, or other tuple that is safe for dictionary can be used as the dictionary key.

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

Conversion method:
T = list (t)
Otherwise:
Arr = tuple (arr)

(2) Dictionary (hash array) Dictionary array

The code is as follows:

# The usage of Dictionary is relatively simple. it can store any value and allow different types of values. the following example describes the usage of Dictionary:
# In the following example, a is an integer, B is a string, and c is an array. this example fully demonstrates the applicability of the hash array.
Dict_arr = {'a': 100, 'B': 'Boys', 'C': ['O', 'P', 'Q']}

# You can add an element directly. if the element has the same name, the value of the element of the original key will be changed.
Dict_arr ['D'] = 'dog'

# Output all keys
Print dict_arr.keys ()

# Output all values
Print dict_arr.values ()

# Traversing arrays
Import types
For k in dict_arr:
V = dict_arr.get (k)
If type (v) is types. ListType: # if the data type is list, continue traversing
Print k ,'---'
For kk, vv in enumerate (v ):
Print kk, vv
Print '---'
Else:
Print dict_arr.get (k)

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.