In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains how to use the sorted () function and sort () method in python. The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the sorted () function and sort () method in python.
1.sort ()
Let's first look at the sort () method. The sort method can only operate on lists, while sorted can be used for all iterable objects.
A = [1,5,3,4,2] a.sort () print (a)
After calling the sort () method, the original list is changed.
2.sorted ()
Then look at the sorted () function.
Sorted (iterable, key=None, reverse=False)
Iterable is an iterable object
Key is the specified sort object, specifying which value is arranged. The parameter type is a function type. (you need to pass in a function)
For example, when sorting the key values in dic_items, the default is to sort by key. You can set this parameter to sort by Value.
Reverse is sorted, False is ascending, and True is descending.
The return value is a list.
3.sorted () operation list a = [1,5,3,4,2] print (sorted (a)) print (a)
Use the sorted function without changing the original list.
The procedure for sorted manipulation of other iterable objects is roughly the same as above.
4.sorted () sort dictionary
Using sorted () to sort the dictionary, you first put the values in the dictionary in an iterable object:
Here, dict1 is converted to dict1.items (), and then passed into the sorted () function.
There are two common ways to write about the parameter key
① parameter key: defined using lambda
Use lambda to define a function that gets the second value of x, where x refers to the element in the iterable object.
Dict1 = {'averse: 1,' baked: 4, 'cached: 2,' dumped: 3} print (sorted (dict1.items (), key=lambda x: X [1], reverse=True))
② parameter key: use itemgetter to directly generate from operator import itemgetterdict1 = {'aqu: 1,' baked: 4, 'cached: 2,' dumped: 3} print (sorted (dict1.items (), key=itemgetter (1), reverse=True))
The same effect can be achieved:
About what itemgetter () is, itemgetter () is a higher-order function, the return value is a function, and itemgetter (1) is equivalent to lambda x: X [1].
If you call it separately, you can see:
From operator import itemgetterprint (itemgetter (1) ([1,2,3]))
As shown in the figure, it gets a value with an index of 1 in the list [1, 2, 3].
Thank you for your reading, the above is the content of "how to use the sorted () function and sort () method in python". After the study of this article, I believe you have a deeper understanding of how to use the sorted () function and sort () method in python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.