sequence Sequence
A sequence (sequence) is a set of ordered objects. A sequence can contain one or more elements, or it can have no elements.
The basic data types we mentioned earlier can be used as sequences of objects. An object can also be another sequence. There are two types of sequences:list (table) and tuple (tuple).
The main difference between list and tuple is that once established, each element of a tuple cannot be changed, and the elements of the list can be changed again.
List
Get the number of list elements:
>>> lst=[' update slow ','python', 5.44, False] >>> Len (LST)4
When referencing access, the index is from 0
the beginning, and be careful not to cross:
>>> lst[0]' update slow '>>> lst[1]'python '>>> lst[2]5.44>>> lst[3]false>>> lst[4]traceback (most recent): '<pyshell#30>' in <module > lst[4]indexerror:list index out of range
Use -1
the index to get the last element directly:
>>> lst[-1]false>>> lst[-2]5.44>>> lst[-3]' Python'>>> lst[-4]' update slow '>>> lst[-5] Traceback (most recent): '<pyshell#35>' in <module> lst[-5]indexerror:list index out of range
Because list is a mutable, ordered table, you can append an element to the end of the list:
>>> lst.append (' add me a ')>>> lst[' update slow 'python ' plus me '
Append multiple elements at once:
>>> Lst.extend (['A','B','C'])>>>lst['Update Slow','python', 5.44, False,'add me one .','A','B','C']
To delete the element at the end of the list, use the pop()
method:
>>> lst.pop ()'C'>>> lst[' update slow ' ' python ' ' add me one . ' ' A ' ' B ']
Deletes the element at the specified location, using the pop(i)
method, where the i
index is located:
>>> lst.pop (0)' update slow '>>> lst['python ' add me a 'a'
The list element is replaced and can be assigned directly to the corresponding index position:
>>> lst[-1]=' >>> lst['python' ' add me a ' '
The list element can also be another list, and the list that is inserted is only one element:
>>> lst.append (lst1)>>> lst['python ' add me one . ' ' - ', ['666'qwer']]> >> Len (LST)6
Tuple
Once a tuple is initialized, it cannot be modified. strings are a special element, so you can perform related operations on tuples.
>>> str=' It's time to go to bed, goodnight! 'print (str[:7]) It's bedtime, good night.
The immutable tuple meaning is that the code is more secure because it is immutable. If possible, you can use tuple as much as possible with tuple.
>>> tuple= ('1','2','3')>>> tuple[0]=6Traceback (most recent): File"<pyshell#62>", Line 1,inch<module>Tuple[0]=6TypeError:'tuple'Object does notSupport Item Assignment
Define an empty tuple:
>>> tuple1=()>>> tuple1 ()
Note that you want to define a tuple with only 1 elements:
>>> tuple2= (666,)>>> tuple2 (666,) # correct >>> tuple3= (666)>>> tuple3666 # error, defined only 666 of this number
Note: tuple-so-called "invariant" is that each element of a tuple, pointing to never change.
>>> l=['CCTV-5','HI']>>> tuple4= ('UFO','HACK', L)>>>Tuple4 ('UFO','HACK', ['CCTV-5','HI'])>>> l[1]='I'm going to change .'>>>Tuple4 ('UFO','HACK', ['CCTV-5','I'm going to change .'])
So try to avoid this kind of apply.
Summary of the day
master list and tuple common methods and precautions.
"Python⑤" Python sequence---list and tuple