0

I have executed following tuple cmp(), but unable to understand the cmp algorithm

a) When both tuple are equal , same data type , same value

>>> t=(1,2)
>>> t1=(1,2)
>>> print cmp(t,t1)
0

b) when 2nd value is bigger

>>> t=(1,2)
>>> t1=(2,2)
>>> print cmp(t,t1)
-1

c) and so on ....

>>> t=(1,2)
>>> t1=('a',2)
>>> print cmp(t,t1)
-1

>>> t=(1,2)
>>> t1=('a',2)
>>> print cmp(t1,t)
1


>>> t=(1,2)
>>> t1=(1,2,3)
>>> print cmp(t,t1)
-1
Ravi Yadav
  • 405
  • 4
  • 16

2 Answers2

0

You don't have tuples in most places. It is the comma operator that makes an object a tuple, using just (...) only groups an expression:

>>> (1)
1
>>> 1,
(1,)
>>> type((1))
<type 'int'>
>>> type((1,))
<type 'tuple'>

As such, all your samples are incorrect, in all you have at least one object that is not a tuple.

Your comparisons are therefor mostly between integers, and integers and strings. Numbers (including integers) always sort before other objects in Python 2. In Python 3, comparing different non-numeric types is an error.

Actual tuple comparison compare the contents in lexicographical order; applying cmp() to each paired up value in the tuple in turn until cmp() returns something other than 0:

>>> a = 1, 2
>>> b = 1, 3  # b is 'larger' because 3 > 2
>>> cmp(a, b)
-1
>>> b = 1, 2  # now equal across all values
>>> cmp(a, b)
0
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

This is expected behaviour there are 3 scenarios:

cmp() returns -1 if x < y
cmp() returns 0 if x == y
cmp() returns 1 if x > y

In your last scenarios

>>> t1=(2)
>>> t=(3)
>>> print cmp(t,t1)
1

They are both integers, a tuple consists of at least one comma. (1,) or (1,2) etc. Since 3 is bigger than 2 it returns 1

>>> t=(3)
>>> t1=('a')
>>> print cmp(t,t1)
-1

Integer and character, in this case a has a bigger asciii value so -1

>>> t1=(2)
>>> t=(2,3)
>>> print cmp(t,t1)
1

The tuple is bigger than the int so 1

>>> t=('a')
>>> t1=(3)
>>> print cmp(t,t1)
1

"a" bigger than 3 so 1, opposite of the other one

>>> t=(2)
>>> t1=(3,2)
>>> print cmp(t,t1)
-1

2 is just an integer.

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
  • The OP expected to compare *tuples* and is asking why the outcomes are unexpected *because they expected to compare tuples*. – Martijn Pieters Sep 23 '15 at 07:35
  • Not done yet with answer give me some time... – Niki van Stein Sep 23 '15 at 07:35
  • If you are going to start with a partial answer, at least give a **correct** partial answer. This part doesn't answer the question, the OP clearly knows how `cmp()` works. – Martijn Pieters Sep 23 '15 at 07:37
  • Yes yes you are right, but typing on a phone is not always handy... I edited my answer... – Niki van Stein Sep 23 '15 at 07:40
  • There are still errors in your answer however. *a tuple consists of at least 2 elements* is absolutely not true, because `1,` is a tuple with one element. – Martijn Pieters Sep 23 '15 at 07:49
  • It depends how you see it, (1,empty) but ok, updated my answer. Thanks for the remarks anyways. – Niki van Stein Sep 23 '15 at 07:51
  • No, there is no `(1, empty)`. Tuples are created by the `,` comma operator. The parentheses are optional, unless the context has assigned another meaning to the comma (such as in a method call) or you want to create a tuple of tuples. `(1,)` is not a tuple with two elements, the second one empty, it is a tuple with one element, grouped in parentheses. See https://docs.python.org/2/reference/expressions.html#parenthesized-forms, https://docs.python.org/2/reference/expressions.html#expression-lists and https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences – Martijn Pieters Sep 23 '15 at 07:57
  • ok thanks! learned something new today :D – Niki van Stein Sep 23 '15 at 07:59
  • cmp() returns -1 if x < y cmp() returns 0 if x == y cmp() returns 1 if x > y; explains most of the doubt ....only query left is what happens if the length of tuple does doesn't match ? – Ravi Yadav Sep 23 '15 at 08:04