Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 28th, 2006, 11:32 AM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile Comparing Tuples and Lists

Comparing two tuples gives expected result
print (1, 2) < (1, 3)  # True, makes sense
Similar thing with two lists
print [1, 2] < [1, 3]  # True
Now a list and a tuple
print [1, 2] < (1, 3)  # True, looks okay
Here comes the oops!
print (1, 2) < [1, 3]  # False, oops!
Does anybody know the why?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jan 28th, 2006, 1:12 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Dietrich
Does anybody know the why?
It would appear that Python considers lists to always be "less than" tuples, no matter what the elements in the two objects:
>>> [1, 2] < (0, 1)
True
>>> [1, 2] < (0,)
True
Likewise, the reverse applies; tuples are considered to be always "greater than" lists.
Arevos is offline   Reply With Quote
Old Jan 28th, 2006, 1:28 PM   #3
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Quote:
Originally Posted by Arevos
It would appear that Python considers lists to always be "less than" tuples, no matter what the elements in the two objects:
>>> [1, 2] < (0, 1)
True
>>> [1, 2] < (0,)
True
Likewise, the reverse applies; tuples are considered to be always "greater than" lists.
Thank you Arevos, that seems to be the interesting conclusion.
Moral, don't compare tuples (pears) with lists (apples)!
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jan 28th, 2006, 2:02 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Unless you convert either or with list()/tuple() >.>

^_^
Sane is online now   Reply With Quote
Old Feb 5th, 2006, 2:34 PM   #5
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
I will stop to be silly after this:
[PHP]# a mixed object list
list1 = [2, 1.99, [1, 2], (1, 2), {'c': 1, 'b': 2}, "bush"]

# sort/compare order seems to be numerics (float or integer), dictionary, list, string, tuple
print sorted(list1) # [1.99, 2, {'c': 1, 'b': 2}, [1, 2], 'bush', (1, 2)][/PHP]
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:04 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC