如何在 Python 3 中使用 cmp()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22490366/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:02:43  来源:igfitidea点击:

How to use cmp() in Python 3?

pythonpython-3.x

提问by BenFire

I cannot get the command cmp()to work.

我无法使命令cmp()起作用。

Here is the code:

这是代码:

a = [1,2,3]
b = [1,2,3]
c = cmp(a,b)
print (c)

I am getting the error:

我收到错误:

Traceback (most recent call last):
  File "G:\Dropbox\Code\a = [1,2,3]", line 3, in <module>
    c = cmp(a,b)
 NameError: name 'cmp' is not defined
[Finished in 0.1s]

采纳答案by DSM

As mentioned in the comments, cmpdoesn't exist in Python 3. If you really want it, you could define it yourself:

正如评论中提到的,cmp在 Python 3 中不存在。如果你真的想要它,你可以自己定义它:

def cmp(a, b):
    return (a > b) - (a < b) 

which is taken from the original What's New In Python 3.0. It's pretty rare -- though not unheard of -- that it's really needed, though, so you might want to think about whether it's actually the best way to do whatever it is you're up to.

它取自原始的 Python 3.0 中的新增功能。不过,这种情况非常罕见——尽管并非闻所未闻——但确实需要它,因此您可能需要考虑这是否真的是做任何事情的最佳方式。

回答by maxin

In Python 3.x you can import operatorand use operator module's eq(), lt(), etc... instead of cmp()

在Python 3.x,然后才能import operator和使用操作模块eq()lt()等...而不是cmp()

回答by Peter Chen

If a or b is a class object, then the above answers will have the compilation error as below: For example: a is Class Clock: File "01_ClockClass_lab16.py", line 14, in cmp return (a > b) - (a < b) TypeError: '>' not supported between instances of 'Clock' and 'Clock'

如果 a 或 b 是类对象,则上述答案将出现如下编译错误: 例如:a 是类时钟:文件“01_ClockClass_lab16.py”,第 14 行,cmp return (a > b) - (a < b) 类型错误:“时钟”和“时钟”的实例之间不支持“>”

Change the type with int() to remove the error:

使用 int() 更改类型以消除错误:

def cmp(a, b): return (int(a) > int(b)) - (int(a) < int(b))

def cmp(a, b): return (int(a) > int(b)) - (int(a) < int(b))

回答by Leo

When the sign is needed, probably safest alternative is using math.copysign:

当需要符号时,可能最安全的替代方法是使用 math.copysign:

import math
ang = -2
# alternative for cmp(ang, 0):
math.copysign(1, ang)

# Result: -1

In particular if ang is of np.float64 type because of depreciation of the '-' operator. Example:

特别是如果 ang 是 np.float64 类型,因为 '-' 运算符的折旧。例子:

import numpy as np

def cmp_0(a, b):
    return (a > b) - (a < b)

ang = np.float64(-2)
cmp_0(ang, 0)

# Result:
# DeprecationWarning: numpy boolean subtract, the `-` operator, is deprecated, 
# use the bitwise_xor, the `^` operator, or the logical_xor function instead.

instead one could use:

相反,可以使用:

def cmp_0(a, b):
    return bool(a > b) - bool(a < b)

ang = np.float64(-2)
cmp(ang, 0)
# Result: -1

回答by Chidi

adding to @maxin's answer, in python 3.x, if you want to compare two lists of tuples aand b

添加到@maxin 的答案中python 3.x,如果您想比较两个元组列表 ab

import operator

a = [(1,2),(3,4)]
b = [(3,4),(1,2)]
# convert both lists to sets before calling the eq function
print(operator.eq(set(a),set(b))) #True