Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the operation method of Python dictionary?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the operation method of Python dictionary". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Problem description

How to perform some calculation operations (such as finding the maximum value, sorting, etc.) in the data dictionary?

Solution

There are the following dictionaries:

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

In order to calculate dictionary values, you usually need to use the zip () function to reverse the keys and values of the dictionary first. For example:

# take out the lowest value key pair min_price = min (zip (stocks.values (), stocks.keys ()) print (min_price) "result: (10.75, 'FB')" # sort by value value stock_sorted = sorted (zip (stocks.values (), stocks.keys ()) print (stock_sorted) "result: [(10.75,' FB'), (37.2, 'HPQ'), (45.23) 'ACME'), (205.55,' IBM'), (612.78, 'AAPL')] ""

When performing these calculations, it is important to note that the zip () function creates an iterator that can only be accessed once. Executing the following code results in an error:

Price_and_name = zip (stocks.values (), stocks.keys ()) min_price = min (price_and_name) print (min_price) "result: (10.75, 'FB')" max_price = max (price_and_name) print (max_price) "result: ValueError: max () arg is an empty sequence" discussion

If you perform ordinary mathematical operations in a dictionary, you will find that they only act on keys, not values. For example:

Min (stocks) # return 'AAPL'max (stocks) # return' IBM'

Maybe you'll try using the dictionary's values () method:

Min (prices.values ()) # return 10.75max (prices.values ()) # return 612.78

This is not what you want, you also want to know the corresponding key information.

You can provide arguments to the key function in the min () and max () functions to get information about the keys corresponding to the minimum or maximum values. For example:

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

But this only returns the key, and if you want to get the value, you have to do a query:

Min_value = stocks [min (stocks, key=lambda k: stocks [k])] # return 10.75max_value = stocks [max (stocks, key=lambda k: stocks [k])] # return 612.78

The previous zip () function scheme solves this problem by "reversing" the dictionary to a sequence of (values, keys) tuples. When comparing two tuples, the value is compared first and then the key (if the value is the same, the size of the key is compared). In this way, you can easily find the maximum value and sort in the dictionary with a simple statement.

This is the end of the content of "what is the operation method of Python dictionary". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report