Click on the above “ Xiaobai studies vision ”, Optional plus " Star standard " or “ Roof placement ”
Heavy dry goods , First time delivery
introduction
Data structures are a key part of any programming language . In order to create robust and good performance products , You have to be very aware of the data structure .
In this paper , We will study Python Important data structure of programming language , Dictionary (Dictionary).
A dictionary is an unordered set of key value pairs . Each entry has a key and a value . A dictionary can be regarded as a list with a special index .
The key must be unique and immutable . therefore , We can use strings 、 Numbers ( Integer or floating point ) Or tuples as keys . Values can be of any type .
Consider a case where we need to store student grades . We can store them in dictionaries or lists .
By using a dictionary , We can provide the student's name ( key ) To check each student's grades . However, in the list , In order to get the grades of specific students , We need an extra list .
The new list contains the names of the students , And in exactly the same order as the grade list .
therefore , In this case , A dictionary is a better choice than a list .
After a brief introduction , Let's dig into the dictionary example . These examples will cover the functions of the dictionary and the functions and methods to operate on it .
1. Create a dictionary
We can provide... Between curly braces 0 One or more key value pairs to create a dictionary .
empty_dict = {}
grades = {'John':'A', 'Emily':'A+', 'Betty':'B', 'Mike':'C', 'Ashley':'A'}
grades
{'Ashley': 'A', 'Betty': 'B', 'Emily': 'A+', 'John': 'A', 'Mike': 'C'}
2. Access value
We access the values in the list by providing an index . Similarly , In the dictionary , Access values by using keys .
grades['John']
'A'
grades.get('Betty')
'B'
3. All worth / All keys
keys Method is used to get all the keys .
grades.keys()
dict_keys(['John', 'Emily', 'Betty', 'Mike', 'Ashley'])
The return object is an iteratable dict_keys object . therefore , We can do it in for Iterate over it in a loop .
Similarly ,value Method returns all values .
grades.values()
dict_values(['A', 'A+', 'B', 'C', 'A'])
We can't dict_keys or dict_values Index on , But you can convert them into lists , And then use the index .
list(grades.values())[0]
'A'
items Method returns the key value pair in the tuple .
grades.items()
dict_items([('John', 'A'), ('Emily', 'A+'), ('Betty', 'B'), ('Mike', 'C'), ('Ashley', 'A')])
4. Update or add items
The dictionary is changeable , So we can update 、 Add or delete items . The syntax for updating or adding items is the same . If there is a given key in the dictionary , Update the value of the existing item . otherwise , A new item will be created ( That is, key value pairs ).
grades['Edward'] = 'B+'
grades['John'] = 'B'
grades
{'Ashley': 'A',
'Betty': 'B',
'Edward': 'B+',
'Emily': 'A+',
'John': 'B',
'Mike': 'C'}
5. Delete the project
We can use del or pop Function to delete an item . We just pass the key to delete the item .
del(grades['Edward'])
grades.pop('Ashley')
'A'
grades
'Betty': 'B', 'Emily': 'A+', 'John': 'B', 'Mike': 'C'}
And del Functions are different ,pop Function returns the value of the deleted item . therefore , We can choose to assign it to a variable .
6. Ergodic dictionary
We can traverse the dictionary . By default , The variant is key based .
for i in grades:
print(i)
John
Emily
Betty
Mike
7. Create a dictionary from the list
We can use lists or tuple lists to create dictionaries .
a = [['A',4], ['B',5], ['C',11]]
dict(a)
{'A': 4, 'B': 5, 'C': 11}
b = [('A',4), ('B',5), ('C',11)]
dict(b)
{'A': 4, 'B': 5, 'C': 11}
8. Copy dictionary
grades = {'John':'A', 'Emily':'A+', 'Betty':'B'}
dict1 = grades
dict2 = grades.copy()
dict3 = dict(grades)
be-all dict1,dict2 and dict3 Both contain and grades Exactly the same key value pair . however ,dict1 It's just pointing to grades In the key / Pointer to value pair . therefore ,grades Any change in will also change dict1.
dict2 and dict3 Is a separate object in memory , So they don't get grades The impact of change .
We need to pay special attention to how to copy dictionaries .
summary
Dictionary yes Python Very important data structure in , In many cases . The examples we give in this article will cover most of the knowledge you need to know about Dictionaries . They will make you feel smooth when using the dictionary .
The good news !
Xiaobai learns visual knowledge about the planet
Open to the outside world
download 1:OpenCV-Contrib Chinese version of extension module
stay 「 Xiaobai studies vision 」 Official account back office reply : Extension module Chinese course , You can download the first copy of the whole network OpenCV Extension module tutorial Chinese version , Cover expansion module installation 、SFM Algorithm 、 Stereo vision 、 Target tracking 、 Biological vision 、 Super resolution processing and other more than 20 chapters .
download 2:Python Visual combat project 52 speak
stay 「 Xiaobai studies vision 」 Official account back office reply :Python Visual combat project , You can download, including image segmentation 、 Mask detection 、 Lane line detection 、 Vehicle count 、 Add Eyeliner 、 License plate recognition 、 Character recognition 、 Emotional tests 、 Text content extraction 、 Face recognition, etc 31 A visual combat project , Help fast school computer vision .
download 3:OpenCV Actual project 20 speak
stay 「 Xiaobai studies vision 」 Official account back office reply :OpenCV Actual project 20 speak , You can download the 20 Based on OpenCV Realization 20 A real project , Realization OpenCV Learn advanced .
Communication group
Welcome to join the official account reader group to communicate with your colleagues , There are SLAM、 3 d visual 、 sensor 、 Autopilot 、 Computational photography 、 testing 、 Division 、 distinguish 、 Medical imaging 、GAN、 Wechat groups such as algorithm competition ( It will be subdivided gradually in the future ), Please scan the following micro signal clustering , remarks :” nickname + School / company + Research direction “, for example :” Zhang San + Shanghai Jiaotong University + Vision SLAM“. Please note... According to the format , Otherwise, it will not pass . After successful addition, they will be invited to relevant wechat groups according to the research direction . Please do not send ads in the group , Or you'll be invited out , Thanks for your understanding ~