Python 为什么 cmp() 有用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15556813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is cmp( ) useful?
提问by Thanakron Tandavas
According to the docand this tutorial,
cmp() returns -1 if x < y
cmp() returns -1 if x < y
and
和
cmp() returns 0 if x == y
cmp() returns 0 if x == y
and
和
cmp() returns 1 if x > y
cmp() returns 1 if x > y
The tutorial also said that
教程还说
cmp() returns the sign of the difference of two numbers
cmp() 返回两个数字之差的符号
I don't really get what sign of the difference of two numbersmeans. Doesn't that mean that it returns a value when the sign of numbers aren't equal? Since...
我真的不明白 两个数字的差异意味着什么。这是否意味着当数字的符号不相等时它会返回一个值?自从...
cmp(80, 100) : -1 # both have positive sign.
cmp(180, 100) : 1 # both also have positive sign.
cmp(-80, 100) : -1
cmp(80, -100) : 1
**Note: code from the tutorial.*
**注意:教程中的代码。*
Despite my confusion in sign differences, I can't really think of why do we need a built-in function to return a value of -1 when x < y.
尽管我对符号差异感到困惑,但我真的想不通为什么我们需要一个内置函数来在 x < y 时返回 -1 的值。
Isn't the function cmp( )easily implemented ? Is there any reason why Python creators keep cmp( )function, or is there any hidden usage of this Python's cmp( )function ?
该功能是不是cmp( )很容易实现?Python 创建者有什么理由保留cmp( )函数,或者这个 Python 的cmp( )函数是否有任何隐藏的用法?
采纳答案by Aaron Hall
Why cmp( ) is useful?
为什么 cmp( ) 有用?
It isn't very useful, which is why it was deprecated (the builtin cmpis gone and builtin sorts no longer accept one in Python 3). Rich comparison methodssupplanted it:
它不是很有用,这就是它被弃用的原因(内置cmp函数已经消失,内置函数在 Python 3 中不再接受)。丰富的比较方法取而代之:
object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)
This allows the <symbol (and other symbols) to be overloaded comparison operators, enabling, for example, subset and superset comparisons of set objects.
这允许<符号(和其他符号)是重载的比较运算符,例如,启用集合对象的子集和超集比较。
>>> set('abc') < set('cba')
False
>>> set('abc') <= set('cba')
True
>>> set('abc') == set('cba')
True
>>> set('abc') >= set('cba')
True
>>> set('abc') > set('cba')
False
while it could enable the above, cmpwouldn't allow the following:
虽然它可以启用上述功能,cmp但不允许以下内容:
>>> set('abc') == set('bcd')
False
>>> set('abc') >= set('bcd')
False
>>> set('abc') <= set('bcd')
False
Toy usage for cmp
玩具用途 cmp
Here's an interesting usage which uses its result as an index (it returns -1 if the first is less than the second, 0 if equal, and 1 if greater than):
这是一个有趣的用法,它使用其结果作为索引(如果第一个小于第二个,则返回 -1,如果相等则返回 0,如果大于则返回 1):
def cmp_to_symbol(val, other_val):
'''returns the symbol representing the relationship between two values'''
return '=><'[cmp(val, other_val)]
>>> cmp_to_symbol(0, 1)
'<'
>>> cmp_to_symbol(1, 1)
'='
>>> cmp_to_symbol(1, 0)
'>'
According to the docs, you should treat cmp as if it wasn't there:
根据文档,您应该将 cmp 视为不存在:
https://docs.python.org/3/whatsnew/3.0.html#ordering-comparisons
https://docs.python.org/3/whatsnew/3.0.html#ordering-comparisons
cmpremoved, equivalent operation
cmp删除,等效操作
But you can use this as the equivalent:
但是您可以将其用作等效项:
(a > b) - (a < b)
in our little toy function, that's this:
在我们的小玩具函数中,就是这样:
def cmp_to_symbol(val, other_val):
'''returns the symbol representing the relationship between two values'''
return '=><'[(val > other_val) - (val < other_val)]
回答by Joe
For sorting sequences of items. When you are sorting a list of items you only need to know one item is greater or less than another item.
用于排序项目序列。当您对项目列表进行排序时,您只需要知道一个项目大于或小于另一个项目。
More info here: http://wiki.python.org/moin/HowTo/Sorting/#The_Old_Way_Using_the_cmp_Parameter
更多信息:http: //wiki.python.org/moin/HowTo/Sorting/#The_Old_Way_Using_the_cmp_Parameter
回答by NPE
I don't really get what does it mean sign of the difference of two numbers.
我真的不明白两个数字的差异是什么意思。
This means: take the difference, and then the sign of that difference. For example, if xand yare two numbers:
这意味着:取差异,然后取差异的符号。例如,如果x和y是两个数字:
x < y=>x - y < 0and the function returns -1.x == y=>x - y == 0and the function returns 0.x > y=>x - y > 0and the function returns 1.
x < y=>x - y < 0并且函数返回-1。x == y=>x - y == 0并且函数返回 0。x > y=>x - y > 0并且函数返回 1。
For more information on three-way comparisons, see Wikipedia.
有关三向比较的更多信息,请参阅Wikipedia。
回答by rra
Trivalued comparators are very useful when sorting. You don't just want to know whether two elements are equal; you also want to know their relative order so that you know how to rearrange them to move closer to a sorted list. This is why C (strcmp) and Perl (cmp) both have similar operations (in those cases for strings, but it's the same idea).
三值比较器在排序时非常有用。您不仅想知道两个元素是否相等;您还想知道它们的相对顺序,以便您知道如何重新排列它们以更接近已排序的列表。这就是为什么 C ( strcmp) 和 Perl ( cmp) 都有相似的操作(在这些情况下是字符串,但它的想法是相同的)。
回答by user1767754
Another use case: Finding the sign (- / +)of a number
另一种使用情况:寻找符号- (/ +)的若干
If you want to find out, what the sign (+/-) of a number is, you can easily use 0as the second argument to the cmpfunction
如果您想知道数字的符号 (+/-) 是什么,您可以轻松地将其0用作cmp函数的第二个参数
cmp(-123, 0) #returns -1
cmp( 123, 0) #returns 1

