How to perform a number of computational operations in the data dictionary (such as seeking minimum, maximum, sorting, etc.)?

problem

How to perform a number of computational operations in the data dictionary (such as seeking minimum, maximum, sorting, etc.)?

solution

Consider the following names and stock prices mapping dictionary:

prices = {
    'ACME': 45.23,
    'AAPL': 612.78,
    'IBM': 205.55,
    'HPQ': 37.20,
    'FB': 10.75
}

To perform dictionary value calculation operation usually requires the use of  zip() function keys and values first reversed. For example, here is the code to find the minimum and maximum stock prices and stock values:

min_price = min(zip(prices.values(), prices.keys()))
# min_price is (10.75, 'FB')
max_price = max(zip(prices.values(), prices.keys()))
# max_price is (612.78, 'AAPL')

Similarly, the use  zip() and  sorted() functions to arrange data dictionary:

prices_sorted = sorted(zip(prices.values(), prices.keys()))
# prices_sorted is [(10.75, 'FB'), (37.2, 'HPQ'),
#                   (45.23, 'ACME'), (205.55, 'IBM'),
#                   (612.78, 'AAPL')]

When performing these calculations should be noted that  zip() function to create an iterator can only be accessed once. For example, the following code will generate an error:

prices_and_names = zip(prices.values(), prices.keys())
print(min(prices_and_names)) # OK
print(max(prices_and_names)) # ValueError: max() arg is an empty sequence

discuss

If you perform common mathematical operations in a dictionary, you will find that they are only acting on the key, rather than the value. such as:

min(prices) # Returns 'AAPL'
max(prices) # Returns 'IBM'

This result is not what you want, because you want to run these calculations on a set of values in the dictionary. You might try using a dictionary of  values() ways to solve this problem:

min(prices.values()) # Returns 10.75
max(prices.values()) # Returns 612.78

Unfortunately, the same is not usually the result that you want. You may also want to know the information of the corresponding key (for example, that the stock price is the lowest?).

You can  min() and  max() provide functions  key function parameters to obtain information corresponding to the minimum or maximum bond. such as:

min(prices, key=lambda k: prices[k]) # Returns 'FB'
max(prices, key=lambda k: prices[k]) # Returns 'AAPL'

However, if you also want to get the minimum, and you have to perform a lookup operation. such as:

min_value = prices[min(prices, key=lambda k: prices[k])]

In front of the  zip() function by the program Dictionary "reverse" is (value, key) tuples to solve the above problems. When comparing two tuples, the value is going to compare, and then the key. So you can pass a simple statement can easily be implemented on the dictionary for the most value and the sort operation.

Note that the use of the (value, key) of the operation in the calculation. When multiple entities share the same values, the key decision will return results. For example, in the implementation  min() and  max() when in operation, if the minimum or maximum value happens to duplicate, then the entity has a minimum or maximum bond returns:

>>> prices = { 'AAA' : 45.23, 'ZZZ': 45.23 }
>>> min(zip(prices.values(), prices.keys()))
(45.23, 'AAA')
>>> max(zip(prices.values(), prices.keys()))
(45.23, 'ZZZ')
>>>
from python cookbook

 

Guess you like

Origin blog.csdn.net/weixin_43866211/article/details/94733063