List1 = [1,2,3,4,5,6,7,8,9,10]
List2 = [' A ', ' B ', ' C ', ' d ', ' e ', ' F ']
Basic Operations1. Delete the specified element
Del List1[2] deleted 3 in list
Support slices: del List1[1:3] to delete 2 and 3 in the list
2. Shard Assignment
1Name = List ('Lixiaolong')2 name3['L','I','X','I','a','o','L','o','N','g']4Name[2:] ='Lianjie'5 name6['L','I','L','I','a','N','J','I','e']
Although later Lilianjie is 1 less than the original Lixiaolong character length, it follows the 4th line of code:
Name[2:] = ' Lianjie ' after the original list length has been changed
List of methods1.count
Used to calculate the number of occurrences of an element
1 List1.count (1) 2 1
2.extend
The Extend method can append the value of another list to the end of the list, extending the existing list
list1.extend (LIST2) [1,2,3,4,5,6,7,8,9,10,'a','b' ,'C','d','e' ,'f']
I know what you're trying to say. This method top and bird use, I a connector + teach you to be a man
The difference between the method and the + is that the method belongs to the in-place operation, and the + will return a copy containing two lists, the original list has not changed
If you use List1 = List1 + List2 method, you will lose efficiency.
Of course, you can do that, too.
List1[len (list1):] = List2
But it's far less readable than this approach.
3.index
Used to find the first matching value from a list
1 list1.index (ten)2 ouput:9
The P.s.:rindex method only has a string, and the list does not
4.insert
The Insert method can insert the specified element before the specified position in the list, so it is required to write two parameters
List1.insert (3,11) output:[1,2,3,11,4,5,6,7,8,9,10]
It can be understood that inserting XX after the first few elements
5.remove
Delete the first matching element in a list
List1.remove (1) output:[2,3,4,5,6,7,8,9,10]
Note: Only one element can be deleted at a time and cannot be deleted in bulk
6.reverse
Reverse-store the list, which belongs to the in-situ operation
list1.reverse () output:[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
7.sort
The sort method is to sort the list by a fixed sort, but be aware that the following are typical error uses for this method:
List1.reverse ()# First reverse-sort list1 # error start list2output:none
List1
OUTPUT:[1,2,3,4,5,6,7,8,9,10]
The list list2 is empty because the list list1 returns a null value of none after calling the sort method without returning the row list List1.
But the list list1 has been sorted
So if you want to get a sorted list and keep the original list, you should do this:
= List1[:]list3.sort () list1
output:[10,9,8,7,6,5,4,3,2,1]
List3
ouput:[1,2,3,4,5,6,7,8,9,10]
List3 = list1[:] The difference from LIST3 = List1 is that the former is a copy of List1, which simply points list3 to List1
In addition, the Sort method provides two keyword parameters (key and reverse) for custom sorting
The key parameter should pass in the sorted method function name, reverse to a Boolean value, to determine whether to reverse sort
A = ['abcd','abc','ab',' abcde]a.sort (key=len,reverse=False) output:['abcde' ,'abcd','abc','ab ']
Python list common methods and basic operations