Python3 notes-list, python3 Notes List
List deduplication methods:
1 # create a list and put data 2 a = [, 2] 3 B = [,] 4 5 d = [] 6 for I in: 7 if I not in d: 8 d. append (I) 9 print (d) 10 11 # set deduplication 12 B = set (B) 13 print (B)
List slice and flip list:
>>> S = 'abcdefgh' >>> s [:-1] # It can be regarded as a flip operation 'hgfedba' >>>> s [:: 2] # an operation to take an element 'aceg'
List sorting: sorted ()
You can retain the original list and get the sorted list sorted () as follows:
>>> a = [5,7,6,3,4,1,2]>>> b = sorted(a)>>> a[5, 7, 6, 3, 4, 1, 2]>>> b[1, 2, 3, 4, 5, 6, 7]
The sorted () method can be used in a sequence of any data type. The returned result is always in the form of a list:
>>> sorted('iplaypython.com')['.', 'a', 'c', 'h', 'i', 'l', 'm', 'n', 'o', 'o', 'p', 'p', 't', 'y', 'y']
Sequence type operators:
Seq [ind]: obtains the element whose subscript is ind.
Seq [ind1: ind2]: gets the element from ind1 to ind2, excluding the element of ind2.
Seq1 + seq2: concatenate sequence 1 and Sequence 2. This method is not the most effective. extend: seq1.extend (seq2)Print (seq1), if it is a string using. join
Obj in seq: determines whether the obj element is included in seq.
Boj not in seq: determines whether the obj element is not included in seq.