[Python ⑤] python sequence --- list and tuple, --- listtuple
Sequence
Sequence is a set of ordered objects. A sequence can contain one or more elements, or contain no elements.
The basic data types we mentioned earlier can all be used as sequence objects. The object can also be another sequence. There are two sequences:List (table) and tuple (tuples ).
The main difference between list and tuple is that, once created, each element of tuple cannot be changed, and each element of list can be changed.
List
Obtain the number of list elements:
>>> Lst = ['Update slowness ', 'python', 5.44, False] >>> len (lst) 4
When the access is referenced, the index is from0First, do not cross the border:
>>> Lst [0] 'slow Update' >>> lst [1] 'python' >>> lst [2] 5.44 >>> lst [3] False >>> lst [4] Traceback (most recent call last): File "<pyshell #30>", line 1, in <module> lst [4] IndexError: list index out of range
Use-1Index to directly obtain the last element:
>>> Lst [-1] False >>>> lst [-2] 5.44 >>>> lst [-3] 'python' >>> lst [-4] 'slow update '>>> lst [-5] Traceback (most recent call last): File "<pyshell #35>", line 1, in <module> lst [-5] IndexError: list index out of range
Because list is a variable ordered table, you can append an element to the end of the list:
>>> Lst. append ('add me one ') >>> lst ['Update slowness', 'python', 5.44, False, 'add me one ']
Append multiple elements at a time:
>>> Lst. extend (['A', 'B', 'C']) >>> lst ['Update slowness ', 'python', 5.44, False, 'add me one ', 'A', 'B', 'C']
To delete the element at the end of the list, usepop()Method:
>>> Lst. pop () 'C' >>> lst ['Update slowness ', 'python', 5.44, False, 'add me A', 'A', 'B']
Deletes an element at a specified position.pop(i)Method, whereiIs the index location:
>>> Lst. pop (0) 'slow update '>>> lst ['python', 5.44, False, 'add me one', 'a']
Replace the list element, which can be directly assigned to the corresponding index location:
>>> Lst [-1] = '000000' >>> lst ['python', 100, False, 'add me one ', '123']
The list element can also be another list. The inserted list is regarded as only one element:
>>> Lst. append (lst1) >>> lst ['python', 5.44, False, 'add me one ', '123', ['123 ', 'qwer '] >>> len (lst) 6
Tuple
Once initialized, Tuple cannot be modified. A string is a special element, so you can perform operations on tuples.
>>> Str = 'It's time to go to bed. Good night! '>>> Print (str [: 7]) It's time to go to bed. Good night
The immutable tuple makes the code safer because it is immutable. If possible, use tuple whenever possible.
>>> tuple=('1','2','3')>>> tuple[0]=6Traceback (most recent call last): File "<pyshell#62>", line 1, in <module> tuple[0]=6TypeError: 'tuple' object does not support item assignment
Define an empty tuple:
>>> tuple1=()>>> tuple1()
Note:To define a tuple with only one element:
>>> Tuple2 = (666,) >>> tuple2 (666,) # correct >>> tuple3 = (666) >>> tuple3666 # incorrect, only 666 is defined.
Note: Tuple's so-called "unchanged" means that every element of tuple,PointAlways unchanged.
>>> L = ['CCTV-5', 'Hi'] >>> tuple4 = ('uvo', 'hink', l) >>> tuple4 ('uff', 'hack', ['CCTV-5', 'Hi']) >>> l [1] = 'change First '> tuple4 ('uff', 'hack', ['CCTV-5', 'change First '])
So try to avoid this application.
Summary of the day
Measure the test taker's knowledge about the common methods and precautions of List and Tuple.
In Python, the List can be changed, and Tuple cannot be changed. In which case should tuple be used?
Tuple is an unchangeable list. Once a tuple is created, it cannot be changed in any way.
The similarities between Tuple and list
Defining tuple is the same as defining list, except that the entire element set is enclosed by parentheses rather than square brackets.
The Tuple elements are sorted in the same order as the list elements. The index of Tuples starts from 0, so the first element of a non-empty tuple is always t [0].
Negative index and list are counted from the end of tuple.
Slice can also be used like list. Note: When a list is split, a new list is obtained. When a tuple is split, a new tuple is obtained.
Tuple does not exist
You cannot add elements to tuple. Tuple does not have the append or extend method.
You cannot delete elements from tuple. Tuple does not have the remove or pop method.
You cannot search for elements in tuple. Tuple does not have the index method.
However, you can use in to check whether an element exists in tuple.
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 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 must convert tuple to list.
Tuple and list Conversion
Tuple can be converted to list, and vice versa. The built-in tuple function receives a list and returns a tuple with the same elements. The list function receives a tuple and returns a list. In terms of effect, tuple freezes a list, while list restores a tuple.
Other Tuple applications
Assign multiple values at a time
>>> V = ('A', 'B', 'E ')
>>> (X, y, z) = v
Explanation: v is a three-element tuple, and (x, y, z) is a three-variable tuple. Assign a tuple value to another tuple, and assign each value of v to each variable in order.
Python list file tuple
F = open('yourfile.txt ', 'w ')
L = f. readline (). split ()
Ll = [(int (l [I]), int (l [I + 1]) for I in range (0, len (l), 2)]
F. close ()