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 does python sort dictionaries by key or numeric value

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how python sorts dictionaries according to keys or values. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.

How to sort dictionaries by key or numerical value # sort dictionaries by key or numerical value dict3 = {"low": 3, "irving": 6, "james": 1, "durant": 4} # Bronze player list3 = sorted (dict3.items (), key=lambda x: X [0]) dict3_keys = {for j for I, j in list3} print ("Bronze player keystroke sort:" Dict3_keys) list3 = sorted (dict3.items (), key=lambda x: X [1]) dict3_values = {isorted j for I, j in list3} print ("Bronze player by value:", dict3_values) # King player dict3_keys = {key: dict3 [key] for key in sorted (dict3)} print ("King player key order:", dict3_keys) dict3_values = {key: dict3 [key] for key in sorted (dict3) Key=dict3.get)} print ("Masters by value:", dict3_values)

Bronze player: this time I used the sorted built-in function to sort it, and then converted it to dictionary form. Two lines of code are done!

King player: bronze player your method will result in a waste of space resources in the process of conversion. I directly use the sorting of keys or values, and then use the sorted keys or values to construct the final dictionary, and the program is simple.

The above is all the content of the article "how to sort dictionaries by key or value by python". Thank you for reading! Hope to share the content to help you, more related knowledge, 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: 210

*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