Home>Article>Backend Development> Detailed explanation of how to sort dictionary elements in a list using Python programming

Detailed explanation of how to sort dictionary elements in a list using Python programming

黄舟
黄舟 Original
2017-05-28 11:12:17 2094browse

This article mainly introducesPythonProgrammingHow to sort dictionary elements in a list, involving Python's operating skills related to traversing, reading, and converting list and dictionary elements , Friends who need it can refer to

The example of this article describes the method of sorting dictionary elements in the list using Python programming. Share it with everyone for your reference, the details are as follows:

Content directory:

1. Origin of the problem
2. Sorting dictionary elements in the list
3. Comparejson(ignoring the order of the dictionaries in the list)

1. Origin of the problem

##json

Objecta,b


a = '{"ROAD": [{"id": 123}, {"name": "no1"}]}' b = '{"ROAD": [{"name": "no1"}, {"id": 123}]}'

Features: The key values corresponding to the keys in the Python objects corresponding to a and b - the lists contain the same dictionary elements , but the only difference is the order. How to tell if two json are equal if order is ignored. Because the dictionary itself sorts itself by key and the list is sorted in the order of joining, it can be easily sorted if you sort the dictionary elements in the list. If the list contains ordinary elements (not a dictionary), the list can be read and sorted through the

list(set()) combination. However, if the list contains dictionary elements, list(set cannot be used) ()) combination, see the prompt:


>>> a = [{'a':1, 'b':2}, {'c':3}] >>> a [{'a': 1, 'b': 2}, {'c': 3}] >>> b = set(a) Traceback (most recent call last): File "", line 1, in  b = set(a) TypeError: unhashable type: 'dict'

The prompt is that the dictionary is a type that cannot be hashed (ordinary non-dictionary elements can be easily arranged by hashing) sequence).

Then the essence of the problem is: how to sort the dictionary elements in the list.

2. Sort the dictionary elements in the list

Fortunately, the list has the

sortedfunction, try it


>>> 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 == qq True >>> p == q False

You can see that it is ok, and you can see that the principle of sorting is the number of elements.

3. Compare json (ignoring the order of dictionaries in the list)


import json def compare_json(a, b): aa = json.loads(a) bb = json.loads(b) len_a = len(aa) len_b = len(bb) 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 True if 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) is wrong, you have to write it as a = '{"road:1}' [Single quotation marks are excluded]

The above is the detailed content of Detailed explanation of how to sort dictionary elements in a list using Python programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn