Python dictionary sorting example

Source: Internet
Author: User
This article mainly introduces the implementation of the python dictionary sorting method. The example analyzes the skills related to the Python dictionary sorting. If you need it, you can refer to the example in this article to analyze the python dictionary sorting method. Share it with you for your reference. The details are as follows:

1. Preparation knowledge:

In python, dictionary is a built-in data type and is an unordered storage structure. Each element is a key-value Pair:
For example, dict = {'username': 'Password', 'database': 'master'}, where 'username' and 'database' are keys, while 'Password' and 'master' are values. You can use d [key] to obtain reference to the corresponding value, but you cannot use value to obtain the key.

Pay attention to the following points for dictionnary:

A. The keys of dictionary are case sensitive;
B. A dictionary cannot contain duplicate keys;
C. dictionary is unordered, and there is no concept of element sequence. They are just simple sequence even.

2. dictionary sorting implementation:

As mentioned above, dictionary itself does not have the concept of sequence, but it is always in some cases. However, we often need to sort the dictionary. How can this problem be solved? The following is an example:

Method 1: sort the elements (key/value pairs) in the simplest way and pick out the values. The items method of the dictionary returns a list of tuples, each of which contains a pair of project-keys and corresponding values. In this case, the sorting method can be sort.

def sortedDictValues1(adict):  items = adict.items()  items.sort()  return [value for key, value in items]

Method 2: Sort keys to pick out values, which is faster than method 1. The keys () method of the dictionary object returns a list of all key values in the dictionary. The order is random. When sorting is required, you only need to use the sort () method for the returned key value list.

def sortedDictValues1(adict):  keys = adict.keys()  keys.sort()  return [adict[key] for key in keys]

Method 3: Use the ing method to perform the last step more effectively.

def sortedDictValues1(adict):  keys = adict.keys()  keys.sort()  return map(adict.get,keys)

Method 4: Sort dictionary keys and return them in the form of a list of tuples. Use the lambda function at the same time;
Sorted (iterable [, cmp [, key [, reverse]
Cmp and key generally use lambda
For example:

>>> D = {"OK": 1, "no": 2} # Sort the dictionary keys and return them in the form of a list of tuples >>> sorted (d. items, key = lambda d: d [0]) [('no', 2), ('OK', 1)]

Sort the dictionary by value and return it in the form of a list of tuples

>>> sorted(d.items, key=lambda d:d[1])[('ok', 1), ('no', 2)]

Although there are many ways to sort dictionary elements, it is not summarized here. However, if you do not have too high requirements on program efficiency, you can choose your preferred method.

I hope this article will help you with Python programming.

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: 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.