Python cookbook (data structure and algorithm) dictionary-related computing example, pythoncookbook

Source: Internet
Author: User

Python cookbook (data structure and algorithm) dictionary-related computing example, pythoncookbook

This example describes the computation problems related to the Python cookbook (data structure and algorithm) dictionary. We will share this with you for your reference. The details are as follows:

Problem:Perform Various calculations (such as minimum, maximum, and sorting) on the data in the dictionary ).

Solution:Exploitationzip()Convert the dictionary's key-value pairs into a value-key pair sequence.

For example, the following dictionary stores the stock name and the corresponding price:

>>> Prices = {'acme ': 45.23, 'aap': 612.78, 'ibm': 205.55, 'hpq': 37.20, 'fb ': 10.75 }>>> prices {'hpq': 37.2, 'ibm ': 205.55, 'fb': 10.75, 'acme ': 45.23, 'aap ': 612.78 }>>> min_price = min (zip (prices. values (), prices. keys () # note the order of parameters in zip (x, y)> max_price = max (zip (prices. values (), prices. keys () >>>> min_price (10.75, 'fb ') >>> max_price (612.78, 'aap') >>>> 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 )) # incorrect usage >>> min_price2 ('aapl ',) >>> max_price2 = max (zip (prices )) # incorrect usage >>> max_price2 ('ibm ',) >>> min_price3 = min (zip (prices. keys (), prices. values () # The zip () parameter order is incorrect. The obtained value is incorrect. >>> min_price3 ('aapl ', 612.78) >>> max_price3 = max (zip (prices. keys (), prices. values () # incorrect zip () parameter order, incorrect value obtained >>> max_price3 ('ibm ', 205.55) >>>

Note thatzip()An iterator is created and 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 comparing (value, key) pairs, multiple entries may have the same value. In this case, the key is used as the basis for determining the result.

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

(The code is from Python Cookbook.)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.