A sequence is a group of elements with sequential relationships.
- One dimensional element vector, element types can be different.
- Sequence of similar mathematical elements:,,...,
- Elements are guided by sequence numbers, and specific elements are accessed through subscripts
1. Sequence type and operation
As a base class, sequence derives data types such as string, list and tuple. They have both commonalities and unique operation capabilities. Their elements have an index relationship of forward increasing and reverse decreasing sequence numbers.
1.1 general operators
Operator | meaning |
x in s | Returns True if x is an element of sequence s; otherwise, returns False |
x not in s | Returns False if x is an element of sequence s, otherwise returns True |
s1+s2 | Connect the two sequences s1 and s2 |
s*n or n*s | Copy sequence s n times |
s[i] | Index, which returns the ith element in the sequence s, i is the sequence number |
s[i:j] or s[i:j:k] | Slice and return the subsequence of elements in sequence s from sequence number i to j (excluding j) (in steps of k) |
Example:
#sequence >>>s=['China',123,'.gov'] >>>'China' in s True >>>'china' not in s True >>>s*2 ['China', 123, '.gov', 'China', 123, '.gov'] >>>s[2] '.gov' >>>s[::-1] ['.gov', 123, 'China'] >>>s='China.gov' >>>s[::-1] 'vog.anihC' >>>s=('China',123,'.gov') >>>s[::-1] ('.gov', 123, 'China')
1.2 general functions
function | meaning |
len(s) | Returns the length of the sequence s |
min(s) | Returns the smallest element of sequence s, and the elements in s need to be comparable |
max(s) | Returns the largest element of the sequence s. The Elements in s need to be comparable |
s.index(x) s.index(x,i,j) | Returns the position where the element x first appears in the sequence s from i to j (excluding j) |
s.count(x) | Returns the total number of occurrences of x in sequence s |
Example:
#General function >>>s='China.gov' >>>len(s) 9 >>>min(s) '.' >>>max(s) 'v' >>>s.index('o') 7 >>>s.index('o',1,7) Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> s.index('o',1,7) ValueError: substring not found >>>s.count('a') 1
2. Tuple type and operation
tuple is one of the sequence types and has the following characteristics:
- Once created, it cannot be modified
- Created using parentheses () or tuple(), elements are separated by commas
- Parentheses () may not be used
- When a tuple contains only one element, add a comma after the element, otherwise the parentheses will be used as operators
#tuple >>>t=(123,456) >>>t (123, 456) >>>t=tuple('abcde') >>>t ('a', 'b', 'c', 'd', 'e') >>>creature='dog','cat','monkey','human' >>>creature ('dog', 'cat', 'monkey', 'human') >>>color=(0x001000,'red',creature) >>>color (4096, 'red', ('dog', 'cat', 'monkey', 'human')) >>>t=(123) >>>type(t) <class 'int'> >>>t=(123,) >>>type(t) <class 'tuple'>
Tuple inherits all the operations of the sequence, and it has no unique operations.
>>>creature='dog','cat','monkey','human' >>>creature[::-1] ('human', 'monkey', 'cat', 'dog') >>>color=(0x001000,'red',creature) >>>color[-1][2] 'monkey'
3. List type and operation
list is also one of the sequence types. It is the most commonly used Python data type and has the following characteristics:
- After creation, it can be modified at will
- Created with square brackets [] or list(), elements are separated by commas
- Each element type in the list can be different without length limit
- The list can contain only one element
#list >>>creature=['dog','cat','monkey','human'] >>>creature ['dog', 'cat', 'monkey', 'human'] >>>creature=['dog'] >>>creature ['dog']
The list also inherits all the operations of the sequence, but it has its unique operation function.
function | meaning |
ls[i]=x | The i-th element of the replacement list ls is x |
ls[i,l,k]=lt | Replace the sub list of elements corresponding to the ls sliced list with the list lt |
del ls[i] | Delete the i-th element of the list |
del ls[i,l,k] | Delete the elements with step k from i to j in the list ls |
ls+=lt | Update the list ls and add the elements in the list lt after ls |
ls*=n | Update the list ls and repeat its elements n times |
Example:
>>>creature=['dog','cat','monkey','human'] >>>creature[2]='tiger' >>>creature ['dog', 'cat', 'tiger', 'human'] >>>creature[::-1] ['human', 'tiger', 'cat', 'dog'] >>>del creature[2] >>>creature ['dog', 'cat', 'human'] >>>color=['red','yellow','green'] >>>creature[1:2]=color >>>creature ['dog', 'red', 'yellow', 'green', 'human'] >>>creature+=color >>>creature ['dog', 'red', 'yellow', 'green', 'human', 'red', 'yellow', 'green']