List function:
to convert a string into a list, for example:
>>> name = list (' hello ') >>> name[' h ', ' e ', ' l ', ' l ', ' O ']
List Basic functions:
1. Change the list: element assignment
Using index markers
>>>x = [1, 1, 1]>>>x[1] = 2>>>x[1, 2, 1]
2. Delete element del Statement implementation
>>>names = [' One ', ' one ', ' one ', ' three ']>>>del names[1]>>>names[' one ', ' three ']
3, Shard Assignment (the first parameter is the starting position of the start Shard, the second parameter is the next position of the end shard)
Modifying a sequence
>>>name = List (' Perl ') >>>name[1:] = List (' Ython ') >>>name[' P ', ' y ', ' t ', ' h ', ' o ', ' n ']
Insert Sequence
>>>num = [1, 5]>>>num[1:1] = [2, 3, 4]>>>num[1, 2, 3, 4, 5]
Delete a sequence
>>>num = [1, 5]>>>num[1:1] = [2, 3, 4]>>>num[1, 2, 3, 4, 5]
4, append function (change the original list) (can be implemented into the stack operation)
add a new object at the end of the list
>>>num = [1, 5]>>>num[1:1] = [2, 3, 4]>>>num[1, 2, 3, 4, 5]
5. Count function Count the number of occurrences of an element in a list
>>>[' to ', ' being ', ' or ', ' not ', ' to ', ' being '].count (' to ') 2>>>x = [[1, 2], 1, 1, [2, 1, [1, 2]]]>>>x . Count (1) 2>>>x.cout ([1, 2]) 1
6, Extend function (modify the original sequence, the connection operation to create a new sequence)
append multiple values from another sequence at the end of the list (extend the original list with a new list)
>>>a = [1, 2, 3]>>>b = [4, 5, 6]>>>a.extend (b) >>>a[1, 2, 3, 4, 5, 6]
Use the Shard operation to implement the above steps
>>>a = [1, 2, 3]>>>b = [4, 5, 6]>>>a[len (a):] = b>>>a[1, 2, 3, 4, 5, 6]
7. Index function Find a value from the list where the first occurrence of a match is indexed
>>>num = [' One ', ' one ', ' one ', ' three ', ' four ', ' five ']>>>num.index[' three ']2>>>num[2] ' three '
8. Insert Function inserts an object into the list
>>> numbers = [1, 2, 3, 5, 6, 7]>>> Numbers.insert (3, ' four ') >>> numbers[1, 2, 3, ' Four ', 5, 6, 7]
9, Pop function (can implement the stack operation)
removes an element from the list (the last element by default), and returns the value of the element
>>> x = [1, 2, 3]>>> x.pop () 3>>> x[1, 2]>>> x.pop (0) 1>>> x[2]
10, remove function (and pop difference, modify the original sequence, but do not return)
To remove the first occurrence of a value in a list
>>> x = [' To ', ' is ', ' or ', ' not ', ' to ', ' is ']>>> x.remove (' is ') >>> x[' to ', ' or ', ' not ', ' to ', ' Be ']
11. The reverse function reverses the elements in the list
>>> x = [1, 2, 3]>>> x.reverse () >>> x[3, 2, 1]
12. Sort function (changes the original list, but does not return)
sort the original list
>>> x = [4, 6, 2, 3, 5, 1]>>> x.sort () >>> x[1, 2, 3, 4, 5, 6]
If you do not need to modify the original sequence (note: if x = y is used here, then X and Y both point to the same list, even if the operation is only for Y sort, in fact X will be sorted)
Method One: Create a copy of Y and sort y
>>> x = [4, 6, 2, 3, 5, 1]>>> y = x[:]>>> y.sort () >>> x[4, 6, 2, 3, 5, 1]>>> Y[1, 2, 3, 4, 5, 6]
method Two: Use the sorted function (return a sorted copy without changing the original sequence)
>>> x = [4, 6, 2, 3, 5, 1]>>> y = sorted (x) >>> y[1, 2, 3, 4, 5, 6]>>> x[4, 6, 2, 3, 5, 1]
keyword sort: key
Length (len) Sort:
>>> x = [' BB ', ' eeeee ', ' a ', ' dddd ', ' CCC ']>>> x.sort (key = len) >>> x[' a ', ' BB ', ' CCC ', ' dddd ', ' Eeeee ']
keyword sort: reverse (Simple Boolean sort, true from large to small, false to large)
>>> x = [4, 6, 2, 3, 5, 1]>>> x.sort (reverse = True) >>> x[6, 5, 4, 3, 2, 1]>>> y = [4, 6, 2, 3, 5, 1]>>> y.sort (reverse = False) >>> y[1, 2, 3, 4, 5, 6]
13. CMP (x, y) function (you can customize the compare (x, y) function to compare two elements)
Compare the size of two functions
>>> CMP ( -1>>>) 1>>> CMP (23, 23) 0
with the sort function, you can customize the sorting, sort (CMP)
Python: List function