- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
cmp() returns -1 if x < y
和
cmp() returns 0 if x == y
和
cmp() returns 1 if x > y
教程里也说了
cmp() returns the sign of the difference of two numbers
我真的不明白 两个数字之差的符号 是什么意思。这是否意味着当数字的符号不相等时它会返回一个值?因为……
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
**注意:教程中的代码。*
尽管我对符号差异感到困惑,但我真的想不出为什么我们需要一个内置函数来在 x < y 时返回值 -1。
不是函数 cmp( )
容易实现? Python 创作者保留 cmp( )
有什么原因吗?函数,或者这个 Python 的 cmp( )
是否有任何隐藏用法 功能?
最佳答案
Why cmp( ) is useful?
它不是很有用,这就是它被弃用的原因(内置 cmp
已消失,内置排序在 Python 3 中不再接受)。 Rich comparison methods取代它:
object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)
这允许 <
符号(和其他符号)是重载的比较运算符,例如,可以对集合对象进行子集和超集比较。
>>> 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
虽然它可以启用上述功能,cmp
不允许以下行为:
>>> set('abc') == set('bcd')
False
>>> set('abc') >= set('bcd')
False
>>> set('abc') <= set('bcd')
False
cmp
的玩具用途这是一个有趣的用法,它使用它的结果作为索引(如果第一个小于第二个,则返回 -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)
'>'
根据文档,您应该将 cmp 视为不存在:
https://docs.python.org/3/whatsnew/3.0.html#ordering-comparisons
cmp
删除,等效操作但是您可以将其用作等效项:
(a > b) - (a < b)
在我们的小玩具函数中,就是这样:
def cmp_to_symbol(val, other_val):
'''returns the symbol representing the relationship between two values'''
return '=><'[(val > other_val) - (val < other_val)]
关于python - 为什么 cmp() 有用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15556813/
当我开始学习一门新语言时,我总是觉得我没有以实用、标准的方式进行学习。所以这里有一个关于 jQuery 的问题以及我所做的是否可以接受。 我有 3 张图片。 然后我让 jQuery 检测 $('
基本上,我想知道线程是否有用或必要,或者可能更具体地说,您将使用它的用途和情况。我对线程了解不多,也从未使用过它(我主要使用 C#),并且想知道如果使用它们是否会提高性能或稳定性。如果有人愿意解释一下
这个问题在这里已经有了答案: What is The Rule of Three? (8 个答案) 关闭 7 年前。 嘿嘿。我有一个让我很难过的问题。我自定义了一个普通的拷贝构造函数但它只在我初始化
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Uses for multiple levels of pointer dereferences? 我在 C
我不确定异常在每种语言中的工作方式是否相同,但我使用的是 PHP,我想知道我什么时候做这样的事情: if (!$this->connection[0]->query($this->query)) t
Scala native 是最近发布的,但是他们(现在)使用的垃圾收集器非常rudimentary,因此不适合严肃使用。 所以我想知道:为什么不只将Scala转换为Go (即Scala.js)?这将是
最近,我一直在研究docker及其对SaaS公司的有用性。我花了一些时间学习如何容器化应用程序,并简要了解了什么是docker和容器。我在理解这项技术的实用性时遇到了一些问题。我看过dockercon
我必须根据出现在它们之前的字符串是否是某个关键字“load”从输入文件中读取整数。没有关键数字告诉我们要输入多少个数字。这些数字必须保存到一个数组中。为了避免为扫描的每个附加数字创建和更新新数组,我想
Deferred 对象具有回调池 doneCallbacks、failCallbacks 和 progressCallbacks。 doneCallbacks 和 failCallbacks(以及方法
这个问题在这里已经有了答案: Is there a case where including the same header twice is actually helpful? (6 个答案) 关
我在C++ Programming Language的书上看到了下面的例子 class Ptr { X* operator->( ); }; voide f(Ptr p) { p-
你能不能: template const T &operator[] (unsigned int x) 我的想法是如果你有一个 map如果有一个包装器类可以让您这样做,那就太好了: obj["Int
根据doc这个tutorial , cmp() returns -1 if x y 教程里也说了 cmp() returns the sign of the difference of two nu
我经常读到 It seem that identity monad is useless. It's not... but that's another topic. 那么谁能告诉我它有什么用? 最佳
我已经知道实现和接口(interface)的基础知识。我不明白什么时候使用接口(interface)。有接口(interface)的要求是什么? 例子: /// Interface demo Inte
在一些 R 函数的主体中,例如 lm,我看到对 match.call 函数的调用。正如其帮助页面所述,当在函数内部使用 match.call 时,会返回指定参数名称的调用;这对于将大量参数传递给另一个
在监督学习中,我有典型的训练/测试分割来学习算法,例如回归或分类。关于无监督学习,我的问题是:训练/测试分割是否必要且有用?如果是,为什么? 最佳答案 这取决于问题、数据集的形式以及用于解决特定问题的
我最近接触到 Javascript 模板并变得非常感兴趣。 我正在使用 MVC 模式构建一个大型 PHP 应用程序。模板由相当棒的 Twig 处理. 我最近遇到了一个 javascript imple
我最近在一个我要重构并拥有的项目中遇到了以下代码行: SomeClass someClass = new SomeClass(); 我这辈子都想不通为什么有人会以这种方式使用泛型。我想出的唯一原因是
亲爱的,我正在阅读这篇关于通过 asp.net 4 中的代码动态添加元标记的帖子 - 但我想问一下对 SEO 有什么好处,静态添加它或者在代码后面添加它没有问题 http://weblogs.asp.
我是一名优秀的程序员,十分优秀!