list =[1,2,3,4,5,6,7]
Like the index of a string, the list index starts from 0, and the list can be intercepted and combined.
Two, access the value in the list
Use the subscript index to access the values in the list, and you can also use square brackets to intercept characters
list1 =[‘physics’,’chemistry’,1997,2000]
list2 =[1,2,3,4,5,,6,7]print(list1[0])print(list2[1:5])
Three, update the list
You can modify or update the data items of the list, you can also use the append() method to add list items
list =[‘physics’,’chemistry’,1997,2000]print(“Value available at index 2:”)print(list[2])
list[2]=2001pirnt(“New value available at index 2:”)print(list[2])
1.**append() **Append a single element to the end of the list. It only accepts one parameter. The parameter can be any data type. The appended element retains the original structure type in the list.
list1 =[‘a’,’b’]
list1.append(‘c’)
list1 output[ ‘a’ , ‘b’ , ‘c’ ]
>>> list1
[ ‘a’ , ‘b’ , ‘c’ ]>>>list2
[ ‘d’ , ‘e’ ]>>> list1.extend(list2)>>> list1
[ ‘a’ , ‘b’ , ‘c’ , ‘d’ , ‘d’ , ‘e’ ]
>>> list1
[ ‘a’ , ‘b’ , ‘c’ , ‘d’ ]>>>list1.insert(1,’x’ )>>>list1
[ ‘a’ , ‘x’ , ‘b’ , ‘c’ , ‘d’ ]
Note: To add two lists, you need to create a new list object, which consumes additional memory, especially when the list is large, try not to use "+" to add the list, but should use the append( of the List as much as possible )method.
>>> list1
[ ‘a’ , ‘x’ , ‘b’ , ‘c’ , ‘d’ ]>>>list2=[ ‘y’ , ‘z’ ]>>>list3=list1+list2
>>> list3
[ ‘a’ , ‘x’ , ‘b’ , ‘c’ , ‘d’ , ‘y’ , ‘z’ ]
Fourth, delete the list element
You can use the del statement to delete elements of the list
Five, python list script operator
The operators of the list pairs + and * are similar to strings, the + sign is used to combine lists, and the * sign is used to repeat lists
Python expression | Result | Description |
---|---|---|
len([1, 2, 3]) | 3 | length |
[1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | Combination |
[' Hi!'] * 4 | ['Hi!','Hi!','Hi!','Hi!'] | Repeat |
3 in [1, 2, 3] | True | Does the element exist in the list |
for x in [1, 2, 3]: print x, | 1 2 3 | Iteration |
Six, python list interception
Python expression | Result | Description |
---|---|---|
L[2] | 'SPAM!' | Read the third element in the list |
L[-2] | 'Spam' | Read the penultimate element in the list |
L[1:] | ['Spam','SPAM!'] | Intercept the list from the second element |
Seven, python list operation functions and methods
Function | Function |
---|---|
list.append('element') | Add an element to the end of the list |
list.extend(['Element 1','2','N']) | Add a new list sequence to the end of the list |
list.insert( K, element) | Add an element to the k position of list |
list.clear() | Clear the list sequence content |
list.copy() | Copy list sequence |
list(seq) | Turn the seq tuple into a list |
list.remove(k) | Remove k from list |
del.list([k]) | Delete the content at position k (empty to empty the entire list) |
list.pop(k) | Extract the content of position k in list |
list.count(k) | Find the number of times the value of k is in the list |
list.index(k,i,j) | Find the position of the first k element between i-j |
list.reverse() | Reverse the list sequence in order |
list.sort() | Sorting list contents in positive order |
# print tup1[1] #Print result: chemistry
# tup1[0]=100 #This modification is illegal
# tup2=list(tup1) #If you want to modify the original ancestor, you must first convert it to a list.
# print tup2 #Print result:['physics','chemistry',1997,2000]
# tup2[2]='1989'
# print tup2 #Print result:['physics','chemistry',1989,2000]
names=('mike','mark','candice','laular','mark','msr')print(names.count('mark')) #Print result: 2print(names.index('msr')) #Print result:
Recommended Posts