Python sorts dictionaries by value

Source: Internet
Author: User
I have used the Xinhua Dictionary. How can I sort the dictionary using the python language? The following describes how to sort the dictionary by value by using Python in this tutorial. For details, refer to the following:

Sort the dictionary by value by sorted

>>> Record = {'A': 89, 'B': 86, 'C': 99, 'D': 100 }>>> sorted (record. items (), key = lambda x: x [1]) [('B', 86), ('A', 89), ('C', 99 ), ('D', 100)]

The first sorted parameter must be iterated. It can be tuple or list.

>>> Items = [(1, 'B'), (1, 'A'), (2, 'A'), (0, 'B '), (0, 'A')] >>> sorted (items) [(0, 'B'), (0, 'A'), (1, 'A '), (1, 'B'), (2, 'A')]

Why (0, 'B') is in front of (0, 'A?

Because uppercase and lowercase letters are arranged in front of each ASCII code, use str. lower () to change the order.

>>> Sorted (items, key = lambda x :( x [0], x [1]. lower ()))
[(0, 'A'), (0, 'B'), (1, 'A'), (1, 'B'), (2, 'A')]

The above content is related to the knowledge about how to sort the dictionary by value in Python. I hope it will be helpful to you!

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.