[Tutor] Sequence Comparison
Terry Carroll
carroll at tjc.com
Mon Jan 12 14:16:08 EST 2004
On Mon, 12 Jan 2004, Hameed Khan wrote:
> >>> (1, 2, 3) < (1, 2, 4)
> True
> # Q.1 why it returns True when 1 is not lower then 1
> and 2 is not lower then 2. only 3 is lower then 4
Because the *sequence* is lower.
Put it another way: is (1,2,3) *equal* to ((1,2,4)? Obviously not. So it
has to be either less than or greater than. Less than is the one that
makes sense.
> >>> 'ABC' < 'C' < 'Pascal' < 'Python'
> True
> # Q.2 what is the procedure of comparison in different
> size of sequences because 'ABC' is greater then 'C' in
> size and 'Pascal' is greater then 'C' but equal to
> 'Python' in size
This construct is kind of confusing, and I would never use it; I would
draw the lines at three comparators for a simple range check.
But this is basically saying
"ABC" < "C" is true; and
"C" < "Pascal" is true; and
"Pascal" < "Python" is also true.
You don't really have to worry about different lengths here, because the
difference shows up within the shortest length of each comparison (i.e.
"ABC" < "C" is essentially just checking "A" <"C"; "C" < "Pascal" is
essentially just checking "C"<"P", etc.).
It would be worth worrying anoy if you were comparing, say "aoto" and
"automatic":
>>> "auto"<"automatic"
True
In such a case, the shorter string is less.
This is pretty much what you'd expect if you went to look something up in
the index of a textbook, right? "ABC" would sort before "C";" "Pascal"
would sort before "Python": "auto" would sort before "automatic".
> >>> (1, 2, 3, 4) < (1, 2, 4)
> True
> # the above 2 question (Q.1 and Q.2) applies to this
> comparison because this tuples have different sizes
Right; the same rule as above applies; Python just compares up to the
first differentce; and 1, 2, 3... is less than 1, 2, 4...
What probably throws you here is either a) you're thinking of (1,2,3,4)
and (1,2,4)
as though they're integers 1234 and 124, in which case you'd expect 1234
to be greater than 124; but they're not integers, so it doesn't work to
think of them like that.
Or maybe, b) it bothers you that tuple (1,2,3,4) is longer ("bigger," if
you will) than (1,2,4), and intuitively looks "greater than" to you.
The thing here is that you're mentally comparing, not the tuples
themselves, but their lengths. If you explicitly check the lengths,
comparing them *is* consistent with that approache:
>>> len((1,2,3,4)) < len((1,2,4))
False
If this is where you're coming from, try separating in your mind the value
of the tuple itself from the value of the tuple's length -- think of the
length as an attribue ot the tuple.
> >>> (1, 2) < (1, 2, -1)
> True
> # i totally didnot understand it
That's a good thing to be confused about. It looks like Python's rule is
that the shorter of two tuples, where the two tuples are equal up to the
shortest of the two tuples, is the lesser of the two. That works
intuitively if the rest of the tuple is positive, but not in cases like
this where it's negative.
I guess the approach here is to hark back to the "what order would you
expect in a book index" idea. Personally, I don't like this feature, and
think it should sort the opposite way.
> >>> (1, 2, 3) == (1.0, 2.0, 3.0)
> True
> # i understand it a little bit that the elements of
> second tuple converted to the type of elements of
> first tuple and then compared but what if we compare
> (1,"string",3) == (1.0,2.0,3.0)
That's false, because (I think), it's converting the 2.0 in the second
tuple to the string "2.0". Someone who knows more of this want to
comment?
> >>> (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
> True
> # may be i will understand it if someone explains me
> the above comparisons
Well, there's a tie comparing the tuples up through the elements 1 and 2.
So the tie breaker is comparing the third elements: ('aa', 'ab') in the
first tuple and ('abc', 'a') in the second. The tie is broken with the
very first subelement comparison: comparing 'aa' to 'abc':
'aa' < 'abc', so therefore:
('aa', 'ab') < ('abc', 'a'), so therefore:
(1, 2, ('aa', 'ab') [, ...] ) < (1, 2, ('abc', 'a') [, ...]),
so therefore:
(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
Does this help?
--
Terry Carroll
Santa Clara, CA
carroll at tjc.com
More information about the Tutor
mailing list