A sequence is a type that is ordered by a member and can be accessed by an subscript offset. Python sequences include: strings, lists, and tuples.
Each element of a sequence can be given an offset, and multiple elements are obtained by slicing the operation. The subscript offset is counted from 0 to the total-1 end.
Sequence type operator
These operators are applicable to all sequence types.
Sequence operator |
Role |
Seq[ind] |
Get the element labeled IND |
SEQ[IND1:IND2] |
Gets the set of elements from Ind1 to Ind2 |
SEQ * Expr |
Sequence repeats expr times |
SEQ1 + SEQ2 |
Connecting sequences seq1 and SEQ2 |
obj in seq |
Determine if the obj element is in seq |
Obj not in seq |
Determines whether the obj element is no longer in SEQ |
Seq[ind] has the following code
>>> LST = [1,2,3,4,5,6]>>> exp ="abcdef">>> tub = ("Apple","Orange","Banana","Watermelon")>>>PrintLST[2]#the element labeled 2 in the Print List3>>>PrintEXP[0]#three sequences are used in the same waya>>>PrintTub[3]watermelon>>>PrintLST[-1]#negative index to end as the starting point6>>>PrintLST[6]#index cannot exceed sequence lengthTraceback (most recent): File"<pyshell#7>", Line 1,inch<module>PrintLST[6]#index cannot exceed sequence lengthIndexerror:list Index out of range>>>Print[1,2,3,4,5,6] [0]#you can not use the sequence assignment directly1>>>Print 'ABCDE'[0]#you can not use the sequence assignment directlyA
There are several usage scenarios that cover the seq[ind]. The positive index offset number is 0 to (the total number of elements-1), starting from the first element index, negative index offset is 1 to negative total number of elements, starting from the tail element index.
However, this indexing method can only index one element, and multiple element indexes use
Sequence[start_index:end_index (: Step)]
Have the following code
>>> LST = [1, 2, 3, 4, 5, 6]>>>Printlst[:]#omit two coordinates from start to finish[1, 2, 3, 4, 5, 6]>>>PrintLst[3:]#omit End coordinate[4, 5, 6]>>>PrintLst[:5]#Omit start coordinates[1, 2, 3, 4, 5]>>>PrintLST[::2]#step is 2, take one[1, 3, 5]>>>PrintLST[::-1]#Reverse Sequence[6, 5, 4, 3, 2, 1]>>>PrintLst[1:5:2]#from coordinates 1 to coordinates 5, take one[2, 4]>>>PrintLst[-5:-3][2, 3]>>>Printlst[-1:100][6]>>>PrintLST[-6:-4]#negative index, not negative output[1, 2]>>>PrintLst[-3:5]#when the positive and negative indexes exist, the coordinates only represent the position and the elements between the positions are intercepted.[4, 5]>>>PrintLST[:100]#index can exceed sequence length[1, 2, 3, 4, 5, 6]>>>PrintLST[-5:100]#index can exceed sequence length[2, 3, 4, 5, 6]
There is one more thing to say about slicing operations, if you use a negative index:
>>> lst = [1,2,3,4,5,6,7]>>> print lst[:-2][1, 2, 3, 4, 5]>>> print lst[:-1][1 , 2, 3, 4, 5, 6]>>> print lst[:0][]
When a negative index is a tail coordinate, we can never intercept the last element, because 1 is the maximum value of the negative index, and if you use 0 it is considered a positive index.
In this case, we can use none to replace the 0 position
Print lst[:none][1, 2, 3, 4, 5, 6, 7]
Connection operator +
The join operator allows multiple sequences to be joined together, but only the same object can be connected. are lists, both strings or tuples
>>> Num_lst = [1,2,3,4]>>> mixup_lst = [567,'ABC', [123,'AAA']]>>> Num_lst +mixup_lst[1, 2, 3, 4, 567,'ABC', [123,'AAA']]>>> string ='abcdef'>>> Num_lst +Stringtraceback (most recent): File"<pyshell#4>", Line 1,inch<module>Num_lst+Stringtypeerror:can only concatenate list ( not "Str") to List
Repeating operator (*)
>>> mixup_lst = [567,'ABC', [123,'AAA']]>>> string ='abcdef'>>> Mixup_lst * 2[567,'ABC', [123,'AAA'], 567,'ABC', [123,'AAA']]>>> String * 3'Abcdefabcdefabcdef'>>> Mixup_lst *Mixup_lsttraceback (most recent): File"<pyshell#9>", Line 1,inch<module>Mixup_lst*Mixup_lsttypeerror:can't multiply sequence by non-int of type'List'
* After the number can only be received, representing the number of repetitions, otherwise it will be wrong
Member relationship operation in, not in
>>> mixup_list = ['a', 123,['x', 1.4,35]]>>>'a' inchmixup_listtrue>>>'x' inchMixup_listfalse>>>'x' inchMixup_list[2]true>>> string ='abcdef'>>>'a' inchstringtrue>>>'BCD' inchstringtrue>>>'Abd' inchStringfalse
These are the actions of in, which determine whether the element belongs to this sequence. The result is the opposite if you use not.
2015/8/30 Python Basics (4): Sequence operators