Dictionary traversal and exchange the key and value of the dictionary
When you encounter such a problem, a piece of data is stored in a dictionary like this. Now you need to get the corresponding key according to the value of the dictionary.
**The name and unit price of fruit are the keys and values of the dictionary respectively. Now please take out the fruit with a unit price greater than 4.5**
fruits = {'banana':3.5,'apple':5,'grape':9,'pear':4.5,'pitaya':6}
Here are some solutions to this problem:
1. Traverse the dictionary (key)
We know that the unit price of fruit is the value of the dictionary. If we want to find the one whose unit price is greater than 4.5, we must traverse the value of the dictionary. If we traverse the dictionary in this way
for key in fruits: print(key,end=' ') # Output result: banana apple grape pear pitaya
You can see. After traversing the whole dictionary in this way, we get its key. Now we add a judgment inside the loop
for key in fruits: if fruits[key] > 4.5: # We use the key to index the dictionary and take out the corresponding key for judgment print(key, end=' ') # apple grape pitaya
Of course, when traversing the keys of the dictionary, we can also get all its keys by sending a message dict.keys() to the dictionary
for key in fruits.keys(): print(key, end=' ') # Output result: banana apple grape pear pitaya
2. Traverse dictionary values
We can get all the values of the dictionary by sending the message dict.values() to the dictionary
print(fruits.values()) # dict_values([3.5, 5, 9, 4.5, 6])
Therefore, we can traverse the values of the dictionary
for value in fruits.values(): print(value, end=' ') # 3.5 5 9 4.5 6
However, we can only find how many fruits with a unit price greater than 4.5, and can't take out the corresponding fruit name.
3. Traverse the keys and values of the dictionary
By sending the message dict.items to the dictionary, you can get a nested list with key and value in a binary
For example:
fruits = {'banana':3.5,'apple':5,'grape':9,'pear':4.5,'pitaya':6} print(fruits.items()) # Output result: dict_items([('banana', 3.5), ('apple', 5), ('grape', 9), ('pear', 4.5), ('pitaya', 6)])
In this way, we traverse its keys and values to judge, and we can take out fruits with a unit price greater than 4.5
for key,value in fruits.items(): if value > 4.5: print(key, end=' ') # apple grape pitaya
4. Exchange the key and value of the dictionary (interview question)
Here is another simple way. Imagine if we can exchange the key and value of the dictionary to get such a dictionary
{3.5: 'banana', 5: 'apple', 9: 'grape', 4.5: 'pear', 6: 'pitaya'}
Now, let's go through the keys of the dictionary directly, and then take out the corresponding value to find the desired name
In fact, it is also very simple to program such a dictionary, which only needs such a line of code
dict(zip(fruits.values(),fruits.keys()))
We can see that this result is obtained after the key and value of the dictionary are interchanged and compressed with zip
for zip in zip(fruits.values(),fruits.keys()): print(zip,end=' ') # Output result: (3.5, 'Banana') (5, 'Apple') (9, 'shape') (4.5, 'pear') (6, 'pitaya')
Therefore, now, we use the constructor syntax dict () to construct the dictionary of the zip object, and we can get a new dictionary after exchanging the keys and values with the original dictionary
fruits = {'banana':3.5,'apple':5,'grape':9,'pear':4.5,'pitaya':6} print(dict(zip(fruits.values(),fruits.keys()))) # You get a new dictionary after exchanging keys and values with the original dictionary # Output result {3.5: 'banana', 5: 'apple', 9: 'grape', 4.5: 'pear', 6: 'pitaya'}
Now, we just need to traverse the keys of the dictionary directly for the new list, and then take out the corresponding value to find the desired name
new_fruits = dict(zip(fruits.values(),fruits.keys())) for price in new_fruits: if price > 4.5: print(new_fruits[price],end=' ') # apple grape pitaya
If it helps you, don't forget to like, collect and pay attention!