62

I cannot get the command cmp() to work.

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]
Community
  • 1
  • 1
BenFire
  • 749
  • 1
  • 5
  • 7

9 Answers9

103

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

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.

DSM
  • 342,061
  • 65
  • 592
  • 494
  • 2
    maybe need to cast the booleans like: def cmp(a, b): return int(a > b) - int(a < b) – Oren Aug 21 '20 at 14:51
  • 2
    Following @Oren's comment, if a and b are numpy variables, the `(a > b)` and `(a b) - bool(a < b)` solves this. – Yohai Devir Jun 23 '21 at 07:01
12

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

Sebastian D'Agostino
  • 1,575
  • 2
  • 27
  • 44
maxin
  • 129
  • 1
  • 2
1

When the sign is needed, probably safest alternative is using 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:

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
Leo
  • 178
  • 1
  • 9
0

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

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
Chidi
  • 901
  • 11
  • 15
0

While in the general case, these are all good replacements for cmp(), for the actual use case given by the original poster, surely

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

or just

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

would work quite well.

Dave Atkinson
  • 46
  • 1
  • 5
0

you could use this easier way

a=[1,2,3]
b=[1,2,3]
c=not(a!=b)
c
True
-1

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'

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

def cmp(a, b):
    return (int(a) > int(b)) - (int(a) < int(b))  
Barmar
  • 741,623
  • 53
  • 500
  • 612
Peter Chen
  • 811
  • 6
  • 4
-1

One simple way is to use a - b and check the sign.

def cmp(a, b):
    return a - b
if a < b, negative

if a = b, zero

if a > b, positive
Hong Z
  • 192
  • 2
  • 5
  • `cmp` should also work on non numeric types. `str` for example. It's important that the function really only returns -1, 0, or 1 for proper interop with legacy code. – John La Rooy Mar 30 '23 at 18:56
-4

This cmp() function works only on Python version 2.x, if you try to use it in version 3.x it will give an error:

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

See the code below:

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

output:

-1

While comparing integers cmp() just performs subtraction of its argument i.e in this case a-b, if subtraction is -ve it returns -1 i.e a<b if subtraction is +ve it returns 1 i.e a>b

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

output:

1

Again:

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

output:

0

when both the arguments are equal i.e a=b, it returns 0 as output. Here, we have passed two string type of values. Here, cmp() compares both the strings character by character and if found same then returns 0.

Taie
  • 1,021
  • 16
  • 29