This chapter mainly explains the list, dictionary and tuple related operations commonly used in Python
I. list
List is one of the most commonly used data types in the future, through which data can be stored, modified and other operations most conveniently
Get elements by subscript
#Define a list first letters = ['a', 'b', 'c', 'd', 'e'] letters[1] #Output: b #The index of the list starts from 0 letters[-1] #Output: e #You can take it upside down,-1 Count down the first element
Slicing: getting multiple elements in a list
letters = ['a', 'b', 'c', 'd', 'e'] #Starting from subscript 1 and before taking subscript 3, excluding subscript 3 letters[1:3] #Output results:['b', 'c'] #Remove the subscript 2 to-1,-1 The last one is not included here-1 letters[2:-1] #Output results:['c', 'd'] letters[0:-1] #If you start from 0, 0 can be left blank. The following is the same as the above letters[:-1] #The output results are:['a', 'b', 'c', 'd'] #Take to the end. If you want to take to the last one, it can't be used-1,Directly ignore the existing letters[1:] #Output results:['b', 'c', 'd', 'e'] #If you add a 2 after it, you will get one every other element under the original condition letters[1::2] #Output results:['b', 'd']
append
letters = ['a', 'b', 'c', 'd', 'e'] letters.append('f') print(letters) #Output results:['a', 'b', 'c', 'd', 'e', 'f'] #Just add a new element at the end of the list
insert
letters = ['a', 'b', 'c', 'd', 'e'] letters.insert(3, 'F') print(letters) #Output results:['a', 'b', 'c', 'F', 'd', 'e'] #Insert an element at subscript 3
modify
letters = ['a', 'b', 'c', 'd', 'e'] letters[0] = 'A' print(letters) #Output results:['A', 'b', 'c', 'd', 'e'] #Modify the element of the specified subscript
delete
letters = ['a', 'b', 'c', 'd', 'e'] #del Delete index element del letters[0] #Output results:['b', 'c', 'd', 'e'] #remove Delete specified element letters.remove('b') #Output results:['a', 'c', 'd', 'e'] #pop Delete last value in list letters.pop() #Output results:['a', 'b', 'c', 'd']
extend
letters = ['a', 'b', 'c', 'd', 'e'] letters2 = [1, 2, 3, 4] letters.extend(letters2) #Output results:['a', 'b', 'c', 'd', 'e', 1, 2, 3, 4] #Both lists letters2 Elements of are added to letters in
Copy
letters = ['a', 'b', 'c', 'd', 'e', ['f', 'g']] letters_copy = letters.copy() print(letters_copy) #Output results:['a', 'b', 'c', 'd', 'e', ['f', 'g']]
It seems that the above method has been successfully copied. But it's not over. Let's try to modify the list after copy
letters = ['a', 'b', 'c', 'd', 'e', ['f', 'g']] letters_copy = letters.copy() letters_copy[5][1] = 'h' print(letters) print(letters_copy) #Output results: #['a', 'b', 'c', 'd', 'e', ['f', 'h']] #['a', 'b', 'c', 'd', 'e', ['f', 'h']]
Let's change the 'g' in the fifth element of letters'copy to 'h', and the result of printing shows that the letters also change.
In fact, copy here is shallow copy, only the first layer of the copy list, and the second layer does not have copy, but points to the same address
The elements in the second list are just a reference to each element in the first list
So the point is, what do we do when we want to copy a list completely? Of course, there's a way
By referring to a copy module and calling the deep copy method in the copy module, deep copy can be realized
import copy letters = ['a', 'b', 'c', 'd', 'e', ['f', 'g']] letters_copy = copy.deepcopy(letters) letters_copy[5][1] = 'h' print(letters) print(letters_copy) #Output results: #['a', 'b', 'c', 'd', 'e', ['f', 'g']] #['a', 'b', 'c', 'd', 'e', ['f', 'h']]
In this way, it can be completely copied.
Statistics count
letters = ['a', 'b', 'c', 'd', 'e', 'b'] print(letters.count('b')) #Output: 2 #Count the number of specified elements in the list
Sort sort
letters = ['a', '#b', '1f', 'c', 'e', 'd', 'bear'] letters.sort() print(letters) #Output results:['#b', '1f', 'a', 'c', 'd', 'e', 'bear'] #Sort the list installation order from New #The order of sorting is, special symbols-->number-->Letter-->Chinese characters #Pay attention here Python 3 Different data types cannot be sorted, otherwise an error will be reported letters = ['a', 'b', 'f', 'c', 'e', 'd', 1, 2] letters.sort() #If you sort a list like the one above, you will get an error #TypeError: unorderable types: int() < str()
reverse
letters = ['a', 'b', 'c', 'd', 'e'] letters.reverse() print(letters) #Output results:['e', 'd', 'c', 'b', 'a'] #Reverse list
Get subscript index
letters = ['a', 'b', 'c', 'd', 'e'] print(letters.index('b')) #Output result: 1 #Gets the subscript location of the specified element
Two, dictionary
A dictionary is a key - value data type. Using a dictionary like ours, we can look up the details of the corresponding page through strokes and letters. A key corresponds to a value.
The syntax is as follows:
info = { 'name01': 'xuande', 'name02': 'Guan Yun Chang', 'name03': 'Zhang Yi De', 'name04': 'Zhao Zi Long', 'name05': 'Ma Meng Qi' }
What are the features of a dictionary
- The dictionary is out of order, so the position of the elements in the dictionary can change every time
- The key in the dictionary must be unique, so the key in the dictionary is naturally duplicated
increase
info = { 'name01': 'xuande', 'name02': 'Guan Yun Chang', 'name03': 'Zhang Yi De', 'name04': 'Zhao Zi Long', 'name05': 'Ma Meng Qi' } info['name06'] = 'Huang Han Sheng' print(info) #Output results:{'name05': 'Ma Meng Qi', 'name01': 'xuande', 'name03': 'Zhang Yi De', 'name06': 'Huang Han Sheng', 'name04': 'Zhao Zi Long', 'name02': 'Guan Yun Chang'}
#You can add a dictionary element by assigning a value to a key that does not exist
modify
info = { 'name01': 'xuande', 'name02': 'Guan Yun Chang', 'name03': 'Zhang Yi De', 'name04': 'Zhao Zi Long', 'name05': 'Ma Meng Qi' } info['name05'] = 'Huang Han Sheng' print(info) #Output results:{'name04': 'Zhao Zi Long', 'name02': 'Guan Yun Chang', 'name01': 'xuande', 'name03': 'Zhang Yi De', 'name05': 'Huang Han Sheng'} #Method is the same as adding. As long as the key value exists, it will be overwritten directly
delete
info = { 'name01': 'xuande', 'name02': 'Guan Yun Chang', 'name03': 'Zhang Yi De', 'name04': 'Zhao Zi Long', 'name05': 'Ma Meng Qi' } #There are many ways to delete #1.use pop Delete, use the most and the best info.pop('name01') #2.del Built in delete function, same as the first one del info['name02'] #3.popitem,This is to randomly delete an element in the dictionary info.popitem() print(info) #Output results:{'name03': 'Zhang Yi De', 'name05': 'Ma Meng Qi'}
lookup
info = { 'name01': 'xuande', 'name02': 'Guan Yun Chang', 'name03': 'Zhang Yi De', 'name04': 'Zhao Zi Long', 'name05': 'Ma Meng Qi' } #in judge key Does it exist? print('name01' in info) #Output results: True #get Obtain key Value print(info.get('name02')) #Output: Guan Yunchang #Ibid. key Value print(info['name02']) #Output: Guan Yunchang #It should be noted that in this way, if key No program will report an error print(info['name12']) #Report errors KeyError: 'name102' #If used get No error will be reported, and it will return None
Related operations of multi level dictionary
heros = { 'Wei':{ 'Cao Cao':['An ambitious person','Life's geometry'], 'Sima Yi':['Stratagem'] }, 'Shu Kingdom':{ 'Liu Bei':['cry'], 'Guan Yu':['One's high morality reaching up to the clouds'] }, 'Wu kingdom':{ 'king of Wu in the Three Kingdoms Era':['Young and promising'], 'Zhou Yu':['He Shengliang'] } } heros['Wei']['Cao Cao'][0] += ',I would rather lose the world' print(heros['Wei']['Cao Cao']) #Output results:['Xiao Xiong, I'd rather be the leader of the world', 'Life's geometry'] #Append content directly to element
Take all keys and values
info = { 'name01': 'xuande', 'name02': 'Guan Yun Chang', 'name03': 'Zhang Yi De', 'name04': 'Zhao Zi Long', 'name05': 'Ma Meng Qi' } #Get all keys print(info.keys()) #Output results: dict_keys(['name04', 'name01', 'name03', 'name02', 'name05']) #Take all values print(info.values()) #Output results: dict_values(['Zhao Zi Long', 'xuande', 'Zhang Yi De', 'Guan Yun Chang', 'Ma Meng Qi'])
setdefault function
info = { 'name01': 'xuande', 'name02': 'Guan Yun Chang', 'name03': 'Zhang Yi De', 'name04': 'Zhao Zi Long', 'name05': 'Ma Meng Qi' } #Dictionary does not exist key Otherwise, return the corresponding key Value print(info.setdefault('name01','Cao meng de')) #Output: Liu Xuande print(info.setdefault('name06','Cao meng de')) #Output: Cao mengde
update function
info = { 'name01': 'xuande', 'name02': 'Guan Yun Chang', 'name03': 'Zhang Yi De', 'name04': 'Zhao Zi Long', 'name05': 'Ma Meng Qi' } info2 = { 'name01':'Cao meng de', 'name10':'Cao Zi Xiao' } #Update the existing key value, and add the key value without info.update(info2) print(info) #Output results:{'name04': 'Zhao Zi Long', 'name05': 'Ma Meng Qi', 'name03': 'Zhang Yi De', 'name01': 'Cao meng de', 'name10': 'Cao Zi Xiao', 'name02': 'Guan Yun Chang'}
items
info = { 'name01': 'xuande', 'name02': 'Guan Yun Chang', 'name03': 'Zhang Yi De', 'name04': 'Zhao Zi Long', 'name05': 'Ma Meng Qi' } print(info.items()) #Output results: dict_items([('name01', 'xuande'), ('name02', 'Guan Yun Chang'), ('name04', 'Zhao Zi Long'), ('name03', 'Zhang Yi De'), ('name05', 'Ma Meng Qi')]) #Dictionary to tuple
Circular traversal dictionary dict
info = { 'name01': 'xuande', 'name02': 'Guan Yun Chang', 'name03': 'Zhang Yi De', 'name04': 'Zhao Zi Long', 'name05': 'Ma Meng Qi' } #Method 1: This is the most basic cycle and the most recommended one for key in info: print(key,info[key]) #Method 2: this loop will turn the dictionary into a list first, and then loop. The performance is much worse than that of the first method. If the data volume is large, it is better not to use it for k,v in info.items(): print(k,v) #Output results: #name01 xuande #name04 Zhao Zi Long #name03 Zhang Yi De #name05 Ma Meng Qi #name02 Guan Yun Chang
Three, tuple
In fact, tuples are similar to lists. They also store a set of numbers. Once created, tuples can't be modified, so they are also called read-only lists
Syntax:
heros =('Cao Cao','Liu Bei','king of Wu in the Three Kingdoms Era')
It has only two methods, one is count and the other is index