Directory
- list, tuple operations
- String manipulation
- Dictionary operations
- Collection operations
- File operations
- Character encoding and transcoding
1. List, tuple operations
Lists are one of the most commonly used data types and can be easily stored, modified, and manipulated by the list.
Definition List
names = ["Zhang San", "John Doe", "pock"]
The data in the list is taken by subscript. Subscript starts at 0
names = ["Zhang San", "John Doe", "Pock"]print (Names[0]) Zhang San Print (names[1]) John Doe Print (names[2]) Pock Note: You can also rewind print (names[-3]) Zhang San print ( NAMES[-2]) John Doe Print (names[-1]) pock
Slices: Take multiple elements (Gu Tou regardless of tail.)
names = ["Zhang San", "John Doe", "Pock", "Madman", "Fool"]print (Names[0:2]) [' Zhang San ', ' John Doe ']print (Names[:3]) [' Zhang San ', ' John Doe ', ' pock ']print (names[1:]) [' John Doe ', ' pock ', ' lunatic ', ' fool ']print (Names[0::2]) [' Zhang San ', ' pock ', ' Fool ']print (Names[::2]) [' Zhang San ', ' pock ', ' fool ']
Append (added in last one)
names = ["Zhang San", "John Doe", "Pock", "Madman", "Fool"]names.append ("SB") print (names)
[' Zhang San ', ' John Doe ', ' pock ', ' lunatic ', ' idiot ', ' SB ']
Insert (insert anywhere)
names = ["Zhang San", "John Doe", "Pock", "Madman", "Fool"]names.insert (2, "old Cold Legs") Names.insert (5, "Bigfoot") print (names) [' Zhang San ', ' John Doe ', ' old cold legs ', ' pock ', ' Madman ', ' Bigfoot ', ' fool ', ' SB '
Modify
names = ["Zhang San", "John Doe", "Pock", "Madman", "fool"]names[2] = ("Leper") print (names) [' Zhang San ', ' John Doe ', ' leper ', ' lunatic ', ' fool ']
Delete
#指定删除下标对应的names = ["Zhang San", "John Doe", "Pock", "Madman", "fool"]del names[4]print (names) [' Zhang San ', ' John Doe ', ' pock ', ' Madman ']# Use Remove to delete only the first occurrence of names = ["Zhang San", "John Doe", "John Doe", "Pock", "Madman", "Fool"]names.remove ("John Doe") print (names) [' Zhang San ', ' John Doe ', ' pock ', ' Madman ' , ' fool '] #使用pop删除时删除列表中的最后一个值names = ["Zhang San", "John Doe", "John Doe", "Pock", "Madman", "Fool"]names.pop () print (names) [' Zhang San ', ' John Doe ', ' John Doe ', ' pock ', ' Madman ']
Copy
names = ["Zhang San", "John Doe", "John Doe", "Pock", "Madman", "fool"]names_copy = Names.copy () print (names_copy) [' Zhang San ', ' John Doe ', ' John Doe ', ' pock ', ' lunatic ', ' fool ']
Statistics
names = ["Zhang San", "John Doe", "John Doe", "Pock", "Madman", "Fool"]print ("John Doe Occurrences:", Names.count ("John Doe")) John Doe Number of occurrences: 2
Sort
names = ["Zhang San", "John Doe", "John Doe", "Pock", "Madman", "Fool", "3", "2"]names.sort () print (names) [' 2 ', ' 3 ', ' fool ', ' Zhang San ', ' John Doe ', ' John Doe ', ' pock ', ' lunatic ']
#3.0 The different data types cannot be sorted together
Get subscript
names = ["Zhang San", "John Doe", "John Doe", "Pock", "Madman", "Fool", "1", "3", "2"]print ("John Doe Subscript is:", Names.index ("John Doe")) John Doe subscript is: 1
#只返回找到的第一个下标
python-Base List Collection Dictionary