Life is trivial, this trivial can not let us lose the logic of life, life is a dish, step by step can obtain the things to get.
-----Hashlinux
List
One of the data types built into Python is the list: lists. A list is an ordered set of elements that can be added and removed at any time.
For example, by listing the names of all the classmates in the class, you can use a list to indicate:
>>> classmates = [' Michael ', ' Bob ', ' Tracy ']>>> classmates[' Michael ', ' Bob ', ' Tracy ']
classmates
A variable is a list. len()
the number of list elements can be obtained by using a function:
>>> Len (classmates) 3
Use the index to access the elements of each position in the list, remembering that 0
the index starts from:
>>> classmates[0] ' Michael ' >>> classmates[1] ' Bob ' >>> classmates[2] ' Tracy ' >>> Classmates[3]traceback (most recent): File "<stdin>", line 1, in <module>indexerror:list index out of range
Python will report a indexerror error when the index is out of range, so make sure the index is not out of bounds and remember that the index of the last element is len(classmates) - 1
.
If you want to take the last element, in addition to calculating the index location, you can also use -1
the index to get the last element directly:
>>> classmates[-1] ' Tracy '
And so on, you can get the bottom 2nd, the bottom 3rd one:
>>> classmates[-2] ' Bob ' >>> classmates[-3] ' Michael ' >>> classmates[-4]traceback (most Recent call last): File ' <stdin> ', line 1, in <module>indexerror:list index out of range
Of course, the 4th to the bottom of the border.
The list is a mutable, ordered table, so you can append an element to the end of the list:
>>> classmates.append (' Adam ') >>> classmates[' Michael ', ' Bob ', ' Tracy ', ' Adam ']
You can also insert elements into the specified position, such as the index number 1
:
>>> Classmates.insert (1, ' Jack ') >>> classmates[' Michael ', ' Jack ', ' Bob ', ' Tracy ', ' Adam ']
To delete the element at the end of the list, use the pop()
method:
>>> classmates.pop () ' Adam ' >>> classmates[' Michael ', ' Jack ', ' Bob ', ' Tracy '
To delete an element at a specified location, using the pop(i)
method, where the i
index is located:
>>> Classmates.pop (1) ' Jack ' >>> classmates[' Michael ', ' Bob ', ' Tracy '
To replace an element with another element, you can assign a value directly to the corresponding index position:
>>> classmates[1] = ' Sarah ' >>> classmates[' Michael ', ' Sarah ', ' Tracy '
The data types of the elements in the list can also be different, such as:
>>> L = [' Apple ', 123, True]
The list element can also be another list, such as:
>>> s = [' Python ', ' Java ', [' asp ', ' php '], ' scheme ']>>> len (s) 4
Note that s
there are only 4 elements, one of which s[2]
is a list, which is easier to understand if it is disassembled:
>>> p = [' asp ', ' php ']>>> s = [' Python ', ' Java ', p, ' scheme ']
To get ‘php‘
can write p[1]
or s[2][1]
, therefore s
can be regarded as a two-dimensional array, similar to three-dimensional, four-dimension ... arrays, but rarely used.
If a list does not have an element, it is an empty list with a length of 0:
>>> L = []>>> len (l) 0
Tuple
Another ordered list is called a tuple: a tuple. Tuple and list are very similar, but once the tuple is initialized, it cannot be modified, for example, the name of the classmate is also listed:
>>> classmates = (' Michael ', ' Bob ', ' Tracy ')
Now, classmates this tuple cannot be changed, and it does not have a append (), insert () method. Other methods of acquiring elements are the same as lists, and you can use them normally, classmates[0]
classmates[-1]
but you cannot assign them to another element.
What is the meaning of immutable tuple? Because the tuple is immutable, the code is more secure. If possible, you can use a tuple instead of a list as much as possible.
The trap of a tuple: when you define a tuple, the elements of a tuple must be determined when defined, such as:
>>> T = (1, 2) >>> T (1, 2)
If you want to define an empty tuple, you can write ()
:
>>> t = () >>> t ()
However, to define a tuple with only 1 elements, if you define this:
>>> t = (1) >>> t1
The definition is not a tuple, it is 1
this number! This is because the parentheses ()
can represent both tuple and parentheses in the mathematical equation, which creates ambiguity, so Python rules that, in this case, the parentheses are calculated, and the result is naturally 1
.
Therefore, only 1 elements of a tuple definition must be added with a comma ,
to disambiguate:
>>> T = (1,) >>> T (1,)
Python will also add a comma when displaying a tuple of only 1 elements, ,
lest you misunderstand the parentheses in the mathematical sense.
Finally, let's look at a "mutable" tuple:
>>> t = (' A ', ' B ', [' A ', ' B ']) >>> t[2][0] = ' X ' >>> t[2][1] = ' Y ' >>> t (' A ', ' B ', [' X ', ' Y '])
This tuple is defined by 3 elements, respectively ‘a‘
, ‘b‘
and a list. Doesn't it mean that once a tuple is defined, it's immutable? Why did you change it later?
Don't worry, let's take a look at the definition when the tuple contains 3 elements:
650) this.width=650; "Src=" http://www.liaoxuefeng.com/files/attachments/ 001387269705541ad608276b6f7426ca59b8c2b19947243000/0 "alt=" tuple-0 "/>
When we put the list element ‘A‘
and ‘B‘
modify ‘X‘
it to and ‘Y‘
after, the tuple becomes:
650) this.width=650; "Src=" http://www.liaoxuefeng.com/files/attachments/ 001387269768140c7d5ca167342402989dfc75343fe900b000/0 "alt=" tuple-1 "/>
On the surface, the elements of a tuple do change, but in fact it is not a tuple element, but a list element. The list that the tuple initially points to is not changed to another list, so the so-called "invariant" of a tuple is that each element of a tuple is directed to never change. That is, pointing ‘a‘
, cannot be changed to point ‘b‘
, point to a list, can not be changed to point to other objects, but the list itself is variable!
After understanding "point to invariant", how do you create a tuple that does not change the content? It is important to ensure that each element of a tuple cannot be changed.
Practice
Please use the index to remove the specified element from the list below:
#-*-Coding:utf-8-*-l = [' Apple ', ' Google ', ' Microsoft ', [' Java ', ' Python ', ' Ruby ', ' PHP '], [' Adam ', ' Bart ' , ' Lisa ']
Summary
List and tuple are Python's built-in ordered set, one variable, one immutable. Choose to use them as needed.
Reference source
the_list.py
the_tuple.py
This article is from the "lake and Laughter" blog, reproduced please contact the author!
Python Resume list your collection, tidy up your thoughts.