How to use cmp() in Python 3?

后端 未结 5 1482
甜味超标
甜味超标 2020-12-01 09:18

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

5条回答
  •  醉梦人生
    2020-12-01 09:55

    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.

提交回复
热议问题