【问题标题】:How to use cmp() in Python 3?如何在 Python 3 中使用 cmp()?
【发布时间】:2014-04-24 18:47:54
【问题描述】:

我无法让命令 cmp() 工作。

代码如下:

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

我收到错误:

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]

【问题讨论】:

  • 在什么情况下不工作?
  • 您使用的是 Python 2.x 还是 3.x? 2.x 有cmp(),但 3.x 没有。我怀疑是 3.x,因为 print 后面的括号。
  • 我正在使用 3.x,抱歉,我更新了帖子以在里面包含输出

标签: python python-3.x


【解决方案1】:

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

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

取自原始What's New In Python 3.0。不过,确实需要它非常罕见(尽管并非闻所未闻),所以您可能想考虑一下它是否真的是做您想做的任何事情的最佳方式。

【讨论】:

  • 可能需要转换布尔值,例如: def cmp(a, b): return int(a > b) - int(a
  • 根据@Oren 的评论,如果a 和b 是numpy 变量,则(a &gt; b)(a &lt;b) 变量可能是numpy.bool_ 类型。 numpy.bool_ 类型不支持减法。使用bool(a &gt; b) - bool(a &lt; b) 可以解决这个问题。
【解决方案2】:

在 Python 3.x 中,您可以使用 import operator 并使用运算符模块的 eq()lt() 等...而不是 cmp()

【讨论】:

    【解决方案3】:

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

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

    特别是如果 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.
    

    可以使用:

    def cmp_0(a, b):
        return bool(a > b) - bool(a < b)
    
    ang = np.float64(-2)
    cmp(ang, 0)
    # Result: -1
    

    【讨论】:

      【解决方案4】:

      添加到@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
      

      【讨论】:

        【解决方案5】:

        虽然在一般情况下,这些都是cmp() 的良好替代品,但对于原始发布者给出的实际用例来说,当然

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

        或者只是

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

        会很好用。

        【讨论】:

          【解决方案6】:

          如果 a 或 b 是类对象, 那么上面的答案将有如下编译错误: 例如:a 是类时钟:

            File "01_ClockClass_lab16.py", line 14, in cmp
              return (a > b) - (a < b)
          TypeError: '>' not supported between instances of 'Clock' and 'Clock'
          

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

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

          【讨论】:

          • 您当然应该在自定义类中定义比较运算符,而不是将所有内容都转换为 int?
          【解决方案7】:

          这个cmp()函数只适用于Python 2.x版本,如果你尝试在3.x版本中使用它会报错:

          NameError: name 'cmp' is not defined
          [Finished in 0.1s with exit code 1]
          

          请看下面的代码:

          a=60
          b=90
          print(cmp(a,b))
          

          输出:

          -1
          

          比较整数时 cmp() 只执行其参数的减法,即在本例中为 a-b,如果减法为 -ve,则返回 -1,即 ab

          a=90
          b=60
          print(cmp(a,b))
          

          输出:

          1
          

          再次:

          a="abc"
          b="abc"
          print(cmp(a,b))
          

          输出:

          0
          

          当两个参数相等时,即 a=b,它返回 0 作为输出。在这里,我们传递了两个字符串类型的值。在这里, cmp() 逐个字符地比较两个字符串,如果发现相同则返回 0。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-06-03
            • 2019-10-22
            • 2014-12-02
            • 2017-10-19
            • 2013-04-17
            • 2016-08-01
            • 1970-01-01
            相关资源
            最近更新 更多