Python programming how to sort a list of dictionary elements in a detailed way

Source: Internet
Author: User
This article mainly describes the Python programming method to sort the dictionary elements in the list, involving Python's traversal, read, and transform related manipulation techniques for lists and dictionary elements, and the required friends can refer to the following

This example describes how Python programmatically sorts dictionary elements in a list. Share to everyone for your reference, as follows:

Content directory:

1. The origin of the problem
2. Sort the dictionary elements in the list
3. Compare JSON (ignore the order of dictionaries in the list)

First, the origin of the problem

JSON object, A, b


A = ' {' road ': [{' id ': 123}, {' name ': ' No1 '}]} ' B = ' {' road ': [{' Name ': ' No1 '}, {' id ': 123} '} '

Features: A/b corresponds to the key value of the key in the Python object-the list contains the same dictionary elements, but the only difference is the order is different. If the order is omitted, how to determine whether two JSON is equal. Because the dictionaries themselves are sorted by their keystrokes, the lists are sorted in the order in which they are added, and if you sort the dictionary elements in the list, you can sort them easily. If the list is a normal element (not a dictionary), the list can be sorted by a combination of list (set ()), and if the dictionary element in the list cannot use the list (set ()) combination, look at the hint:


>>> a = [{' A ': 1, ' B ': 2}, {' C ':3}]>>> a[{' a ': 1, ' B ': 2}, {' C ': 3}]>>> B = Set (a) Traceback (most Recent: File "<pyshell#2>", line 1, in <module>  B = Set (a) typeerror:unhashable type: ' Dict '

Tip A dictionary is a type that is not hashed (ordinary non-dictionary elements are hashed to make it easy to sequence).

So the nature of the problem is how to sort the dictionary elements in the list.

Second, sort the dictionary elements in the list

Fortunately, the list has sorted function, try


>>> p = [{' B ': 2}, {' A ': 1, ' C ': 3}]>>> q = [{' A ': 1, ' C ': 3}, {' B ': 2}]>>> p[{' B ': 2}, {' A ': 1 , ' C ': 3}]>>> q[{' A ': 1, ' C ': 3}, {' B ': 2}]>>> pp = sorted (p) >>> qq = sorted (q) >>> pp[ {' B ': 2}, {' A ': 1, ' C ': 3}]>>> qq[{' B ': 2}, {' A ': 1, ' C ': 3}]>>> pp = = qqtrue>>> p = = Qfalse

As can be seen, OK, and you can see the principle of sorting is the number of elements.

Third, compare JSON (ignore the order of dictionaries in the list)


Import Jsondef Compare_json (A, b):  AA = Json.loads (a)  BB = json.loads (b)  len_a = Len (aa)  len_b = Len (BB) C6/>if len_a! = Len_b:    return false  else: for key in    AA:      if not Bb.has_key (key):        return False      else:        if sorted (Aa[key])! = sorted (Bb[key]):          return False  return trueif name = = "Main":  a = ' { "Road": [{"id": 123}, {"Name": "No1"}]} '  B = ' {"Road": [{"Name": "No1"}, {"id": 123}]} '  print Compare_json (A, B)

Details: When writing the JSON format yourself, a = "{' Road ': 1}" Json.loads (a) error, must be written as a = ' {"road:1} '" Single quote Out "

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.