List
Create a list
1. Create directly
>>> a = [1, ' 1 ', ' 1.0 ']
>>> A
[1, ' 1 ', ' 1.0 ']
2. Create with List
>>> A = list ([1, ' 1 ', ' 1.0 ']) >>> A [1, ' 1 ', ' 1.0 '] #括号内可以用小括号, brackets, curly braces.
Method
1. Number of elements in count statistics list
>>> a
[1, 2, 3, 1, 1, 2, 3]
>>> A.count (1)
3
2. Index finds the location of the list where the element is located (the same element in the list returns the first position)
>>> A
[1, 2, 3, 1, 1, 2, 3]
>>> A.index (1)
0
>>> A.index (2)
1
3. The X in list determines if there are elements in the table, returning True or false
>>> 1 in a
True
>>> 5 in a
False
4. Append add elements to the end of list
>>> a = list ([1,2,3,1,1,2,3])
>>> A.append (4)
>>> A
[1, 2, 3, 1, 1, 2, 3, 4]
5. Insert add element at specified position in list
>>> a.insert (0, 5)
>>> A
[5, 1, 2, 3, 1, 1, 2, 3, 4]
6. Extends () adds the value of the B list after the A list
>>> B = [5, 6, 7]
>>> A.extend (b)
>>> A
[5, 1, 2, 3, 1, 1, 2, 3, 4, 5, 6, 7]
7. Modifying list values
>>> a[0] = ' 5 '
>>> A
[' 5 ', 1, 2, 3, 1, 1, 2, 3, 4, 5, 6, 7]
8. Delete list values
Pop () Delete the element and return the deleted value by default delete the last element, you can specify a location to delete
>>> A.pop ()
7
>>> A.pop (0)
' 5 '
Remove () deletes the specified element when there are multiple identical elements when the first one in the list is deleted
>>> A
[1, 2, 3, 1, 1, 2, 3, 4, 5, 6]
>>> A.remove (3)
>>> A
[1, 2, 1, 1, 2, 3, 4, 5, 6]
Del Specify location delete element or delete list
>>> A
[1, 2, 1, 1, 2, 4, 5, 6]
>>> del A[1]
>>> A
[1, 1, 1, 2, 4, 5, 6]
>>> del a
>>> A
Traceback (most recent):
File "<pyshell#49>", line 1, in <module>
A
Nameerror:name ' A ' is not defined
Clear () empties the elements in the list
' >>> a
[1, 1, 1, 2, 4, 5, 6]
>>> A.clear ()
>>> A
[]
Python Learning Notes (4) collections, tuples, dictionaries, collections