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
3
  • (1) is just 1. One element tuple looks like (1,). Commented Sep 23, 2015 at 7:29
  • For readability it does not help that you change the order of t and t1 declarations all the time.. Commented Sep 23, 2015 at 7:54
  • i have updated the question and order..hope it makes things clear Commented Sep 23, 2015 at 8:01

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Martijn, I will update my question .
@user3812837: and what remains is a duplicate question, I'm afraid.
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.

9 Comments

The OP expected to compare tuples and is asking why the outcomes are unexpected because they expected to compare tuples.
Not done yet with answer give me some time...
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.
Yes yes you are right, but typing on a phone is not always handy... I edited my answer...
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.