Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to sort the list of dictionaries through itemgetter in Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to sort the list of dictionaries through itemgetter in Python. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

We have a dictionary list and want to sort the list according to the values in one or more dictionaries.

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

Example:

From operator import itemgetterrows= [{'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 also supports multiple keys rows_by_name_age=sorted (rows,key=itemgetter (' name','age')) print (rows_by_name_age) # itemgetter also applies to min, maxprint (min (rows,key=itemgetter ('uid')) print (max (rows,key=itemgetter (' age')

Running 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'}

Discuss

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

The parameters accepted by the function operator.itemgetter () can be used as tags for the query to extract the desired values from the records of the rows. It can be the key name of the dictionary, a list element represented by a number, or any value that can be passed to the object's _ _ getitem__ () method. If multiple tags are passed to itemgetter (), the callable object it produces will return a tuple containing all elements, and sorted () will then arrange the output based on the sort result of the tuple. This is useful if you want to sort multiple fields at the same time.

Sometimes the functions of itemgetter () are replaced by lambda expressions, such as:

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

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

The above is how to sort the dictionary list through itemgetter in Python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report