In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the difference between sort () and sorted () in python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Sorted (): the first parameter iterable of this function is any object that can be iterated, cmp is a comparison function, usually a lambda function, key is a keyword for comparison, and reverse indicates whether the sorting result is reversed.
L.sort (): the three arguments of this function have the same meaning as the last three arguments of sorted (), and it is important to note that this function only applies to lists, not any objects that can be iterated over. Cmp is a comparison function that takes two object parameters x and y and returns a negative number (xy).
Let's look at this through an example:
L.sort ()
A = [3, 6, 1, 5, 4, 2]
A.sort ()
One thing to note here is that after calling L.sort () to finish sorting, the structure of the list to be sorted is changed, that is, An is [1, 2, 3, 4, 5, 6] after sorting.
We can specify keyword sorting, such as:
Student = [['Tom',' Aids, 20], ['Jack',' Clippers, 18], ['Andy',' Bundles, 11]]
Student.sort (key=lambda student: student [2])
The student list contains a list, and each list is the name, grade, and age of the student. Key is specified as the student's age during the sorting process, so the ranking result is: [['Andy',' accounts, 11], ['Jack',' accounts, 18], ['Tom',' Aids, 20]].
In addition to key, sorting by age can also be achieved by defining the cmp function:
Student = [['Tom',' Aids, 20], ['Jack',' Clippers, 18], ['Andy',' Bundles, 11]]
Student.sort (cmp=lambda x, y: X [2]-y [2])
Sorting can be done by defining both key and cmp, but what's the difference between the two? The function passed in by cmp will be called many times during the sorting process, because it will be compared many times, so the function call is more expensive, so using key is more efficient than cmp.
Sorted ()
Sorted () can be applied to any iterative object, so the scope of application is much wider than L.sort (). It can be applied to string, tuple, list, dictionary and other iterable objects.
B = 'Python'
Sorted (B)
C = (3,6,1,5,4,2)
Sorted (C)
D = [3,6,1,5,4,2]
Sorted (D)
E = {'1percent:' asides, '2percent:' baked, '0percent:' c'}
Sorted (E.items ())
It is important to note that this function returns a sorted list, and the original iterable objects remain the same, unlike the L.sort () function. However, this will waste a large amount of storage space, especially when the amount of data is large. Therefore, when sorting the list, you need to consider whether you need to save the original list, and if you do not need to save the original list, use L.sort () first to save memory space and improve efficiency.
Some sort may need to be based on two keywords, such as the sort of words, the first letter is the same and then sort according to the second letter, does the sorted () function in Python support this implementation? The answer is yes. You can specify more than one attribute when you set key:
L = ['cat',' binary', 'big',' dog']
Print sorted (L, key=lambda x: (x [0], x [1], x [2]))
The result of the sort is ['big',' binary', 'cat',' dog'].
Summary
The L.sort () function applies only to list sorting, while the sorted () function applies to any object sorting that can be iterated.
The sorting of the L.sort () function changes the original list to be sorted, while the sorted () function does not. So when using lists for sorting, you need to consider whether you need to save the original list, and if you don't need to save the original list, use L.sort () first to save memory space and improve efficiency.
Both functions can be sorted by defining key and cmp, but key is much more efficient than cmp, so key is preferred.
These are the differences between sort () and sorted () in python shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, 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.
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.