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

What is the cmp parameter of the sort () method in python

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

Share

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

Editor to share with you what the cmp parameters of the sort () method in python are, I believe most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.

"if you want elements to be sorted in a specific way (instead of the default way of the sort function, that is, to sort elements in ascending order according to the default collation of python), you can customize the comparison function in the form of compare (xmemy).

The compare (xpene y) function returns a positive number when xy, and 0 (as you define it) if xrecoy.

Once the function is defined, it can be provided to the sort method as a parameter. The built-in function cmp provides the default implementation of comparison functions:

For the sort () method, if no parameter is passed, and the default cmp is None, that is, numbers.sort () = number,sort (cmp) = number.sort (cmp=None), we can do a verification:

If you pass in the parameter, numbers.sort (cmp=1), we can verify whether the parameters of the integer can:

Code error: the int object is not a callable, but a callable object. The callable (object) method is used to detect whether the object is callable, that is, whether it is a function, and if so, the value of the function is called and assigned to cmp.

So how do we customize a comparison function?

The rule is: pairwise comparison, if the return is positive, then exchange the position of the two, that is, y before x, otherwise x before y. It can also be explained in this way that ascending order is comparing the first number to the following number, and descending order is comparing the last number to the previous number.

Of course, you can also write numbers.sort (mycmp1) and omit cmp.

We can also go directly into the anonymous function lambda:

In addition, the cmp parameter is removed in python3.x, and the function is not supported to be passed directly to sort (), but the sorting function can be constructed and passed to key to implement.

This is all the content of the article "what are the cmp parameters of the sort () method in python?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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: 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