is an example of Python Cookbook (data structure and algorithm) dictionary related computing. I want to share with you the following details:

problem: performs various calculations on data in dictionary, such as finding minimum value, maximum value and sorting.

solution: uses zip () to "reverse" the key - value of the dictionary to a value - key pair.

例如:如下字典存放的股票名称和对应的价格:

 >>> prices = {  'ACME': 45.23,  'AAPL': 612.78,  'IBM': 205.55,  'HPQ': 37.20,  'FB': 10.75 } >>> prices {'HPQ': 37.2, 'IBM': 205.55, 'FB': 10.75, 'ACME': 45.23, 'AAPL': 612.78} >>> min_price = min(zip(prices.values(), prices.keys())) #注意zip(x,y)中参数的顺序 >>> max_price = max(zip(prices.values(), prices.keys())) >>> min_price (10.75, 'FB') >>> max_price (612.78, 'AAPL') >>> prices_sorted = sorted(zip(prices.values(), prices.keys())) >>> prices_sorted [(10.75, 'FB'), (37.2, 'HPQ'), (45.23, 'ACME'), (205.55, 'IBM'), (612.78, 'AAPL')] >>> min_price2 = min(zip(prices))  # wrong usage of > > > min_price2 ('AAPL', >); > > max_price2 = max (ZIP (prices)) # wrong > > > max_price2 ('IBM', >); > > min_price3 = min (ZIP (prices.keys), prices.values () ()) (#zip) parameters are in the wrong order, get the error value of > > > min_price3 ('AAPL', 612.78) > > > max_price3 = max (ZIP (prices.keys), (prices.values) (#zip)) () out of order parameters, get the error value of > > > max_price3 ('IBM' 205.55, >); > > 

of these calculations, please note that the zip () to create an iterator, its content can only be consumed once. For example:

 > > > pirces_and_names=zip (prices.values), (prices.keys) (>); > > pirces_and_names < zip; object at 0x023BDFA8> > > > min (pirces_and_names) (10.75,'FB') > > > max (pirces_and_names) Traceback (most recent call last): File < pyshell#25> line 1, in, < module> max (pirces_and_names) ValueError: (max) arg is an empty sequence > > > 

note: when it comes to (value, key) for comparison, happened to have multiple entries with the same the value of value, then key will be used as a result of the basis.

 > > > 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') > > 

; >

("Python Cookbook")

more Python on the relevant content of interested readers can see the special station: "Python data structure and algorithm tutorial " and "Python function using techniques" and "Python " string manipulation skills summary, "Python entry and advanced tutorial classic " and "Python

operation skills summary" the hope for all of Python program design Help。

you might be interested in this article:


This concludes the body part

This paper fixed link:http://www.script-home.com/examples-of-python-cookbook-data-structure-and-algorithm-dictionary-related-computing.html | Script Home | +Copy Link

Article reprint please specify:Examples of Python Cookbook (data structure and algorithm) dictionary related computing | Script Home

You may also be interested in these articles!