Home  >  Article  >  Backend Development  >  Python sort dictionary list by common key (using itemgetter function)

Python sort dictionary list by common key (using itemgetter function)

不言
不言forward
2018-10-20 14:48:102508browse

The content this article brings to you is about Python sorting dictionary lists by public keys (using the itemgetter function). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Requirements

We have a list of dictionaries and want to sort the list based on the values ​​in one or more dictionaries.

2. Solution

It is very simple to use the itemgetter function in the operator module to sort such structures.

Example:

from operator import itemgetter
rows=[
{'name':'mark','age':18,'uid':'110'},
{'name':'miaomiao','age':28,'uid':'150'},
{'name':'miaomiao','age':8,'uid':'150'},
{'name':'xiaohei','age':38,'uid':'130'},
]

rows_by_name=sorted(rows,key=itemgetter('name'))
rows_by_uid=sorted(rows,key=itemgetter('uid'))
print(rows_by_name)
print(rows_by_uid)


#itemgetter还支持多个键
rows_by_name_age=sorted(rows,key=itemgetter('name','age'))
print(rows_by_name_age)

#itemgetter同样适用min、max
print(min(rows,key=itemgetter('uid')))
print(max(rows,key=itemgetter('age')))

Run result:

[{'name': 'mark', 'age': 18, 'uid': '110'}, {'name': 'miaomiao', 'age': 28, 'uid': '150'}, {'name': 'miaomiao', 'age': 8, 'uid': '150'}, {'name': 'xiaohei', 'age': 38, 'uid': '130'}]
[{'name': 'mark', 'age': 18, 'uid': '110'}, {'name': 'xiaohei', 'age': 38, 'uid': '130'}, {'name': 'miaomiao', 'age': 28, 'uid': '150'}, {'name': 'miaomiao', 'age': 8, 'uid': '150'}]
[{'name': 'mark', 'age': 18, 'uid': '110'}, {'name': 'miaomiao', 'age': 8, 'uid': '150'}, {'name': 'miaomiao', 'age': 28, 'uid': '150'}, {'name': 'xiaohei', 'age': 38, 'uid': '130'}]
{'name': 'mark', 'age': 18, 'uid': '110'}
{'name': 'xiaohei', 'age': 38, 'uid': '130'}

Discussion

In this example, rows are passed to the built-in sorted() function, which The function accepts a keyword argument key, which should represent a callable object that accepts a single element from rows as input and returns a value used for sorting. The itemgetter() function creates such a callable object.

The parameters accepted by the function operator.itemgetter() can be used as query markers to extract the required values ​​​​from the rows records. It can be a dictionary key name, a numeric list element, or any value that can be passed to the object's __getitem__() method. If you pass multiple tags to itemgetter(), the callable object it generates will return a tuple containing all the elements, and then sorted() will arrange the output results according to the sorting result of the tuple. This is very useful if you want to sort on multiple fields at the same time.

Sometimes lambda expressions are used to replace the function of itemgetter(), for example:

rows_by_uid=sorted(rows,key=lambda r:r['uid'])
rows_by_name_age=sorted(rows,key=lambda r:(r['name','age']))

This solution usually works fine. But using itemgetter() usually runs faster. Therefore, if you need to consider performance issues, you should use itemgetter().

The above is the detailed content of Python sort dictionary list by common key (using itemgetter function). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete