min/max of a mixed type list

Thread Solved

Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

min/max of a mixed type list

 
0
  #1
Jan 10th, 2007
I took a mixed type list and set out to find the min and max values. The results are very surprising to me:
[php]mixed_list = [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789]
mn = min(mixed_list)
mx = max(mixed_list)
print mn, type(mn) # 7 <type 'int'>
print mx, type(mx) # Zoe <type 'str'>
[/php]How does Python handle mixed type lists to get such a result?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,000
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 283
woooee woooee is offline Offline
Veteran Poster

Re: min/max of a mixed type list

 
0
  #2
Jan 10th, 2007
Originally Posted by sneekula View Post
How does Python handle mixed type lists to get such a result?
Pretty much, all pc's use ascii encoding and sort in "ascii order", see http://www.asciitable.com/
Last edited by woooee; Jan 10th, 2007 at 5:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: min/max of a mixed type list

 
0
  #3
Jan 11th, 2007
Originally Posted by sneekula
I took a mixed type list and set out to find the min and max values.
What results have you expected? Is "Dick" larger than 12 or smaller? (I mean the string "Dick", ... ).
Here's what the docu says:
Originally Posted by Python docu
Note that comparing objects of different types is legal. The outcome is deterministic but arbitrary: the types are ordered by their name. Thus, a list is always smaller than a string, a string is always smaller than a tuple, etc. Mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc.
And in a footnote:
Originally Posted by Python docu footnote
The rules for comparing objects of different types should not be relied upon; they may change in a future version of the language.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: min/max of a mixed type list

 
0
  #4
Jan 11th, 2007
Thanks mawe,

originally I thought it was by ascii value, but then 11 would have been the min value since ascii 1 is lower than ascii 7. So the order in the list was more like:

[7, 9, 11, 12, 700, 777, 13456789, 'Dick', 'Mary', 'Paul', 'Zoe']
Is there a way to get a min/max of each type?
Last edited by sneekula; Jan 11th, 2007 at 4:24 am.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: min/max of a mixed type list

 
0
  #5
Jan 11th, 2007
Well, here's the first thing that came to my mind:
  1. In [17]: lst = [5, "Paul", 4, 2, "Mary", 1, "Dick", "Zoe", 3]
  2. In [18]: min_num = min( i for i in lst if isinstance(i, int) )
  3.  
  4. In [19]: min_str = min( i for i in lst if isinstance(i, str) )
  5.  
  6. In [20]: min_num
  7. Out[20]: 1
  8.  
  9. In [21]: min_str
  10. Out[21]: 'Dick'
It works, but there must be something more clever. I'll think about it ...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,000
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 283
woooee woooee is offline Offline
Veteran Poster

Re: min/max of a mixed type list

 
0
  #6
Jan 11th, 2007
originally I thought it was by ascii value, but then 11 would have been the min value since ascii 1 is lower than ascii 7.
11 is only less than 7 on the planet Bizarro, and any number AFAIK is sorted before letters so they are all first, because any digit is less than any character. Note that there is a difference between 11 (decimal 11) and '11' (decimal 49, decimal 49) and they will be sorted differently.
Last edited by woooee; Jan 11th, 2007 at 11:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: min/max of a mixed type list

 
0
  #7
Jan 12th, 2007
Originally Posted by sneekula View Post
I took a mixed type list and set out to find the min and max values. The results are very surprising to me:
[php]mixed_list = [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789]
mn = min(mixed_list)
mx = max(mixed_list)
print mn, type(mn) # 7 <type 'int'>
print mx, type(mx) # Zoe <type 'str'>
[/php]How does Python handle mixed type lists to get such a result?
another way...but you have to test it in different cases...
  1. lst = [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789]
  2. slist = sorted(lst) #sort the list
  3. min_num , max_str = slist[0], slist[-1] #first and last element usually is minnum, max str.
  4. for num,item in enumerate(slist):
  5. try:
  6. if item.isalpha(): #check for first occurence of a word.
  7. min_str = item
  8. max_num = slist[num-1] #get the number on the left of this word. Usually is max num
  9. break
  10. except: pass
  11. print min_num ,max_num , max_str , min_str
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: min/max of a mixed type list

 
0
  #8
Jan 12th, 2007
If a sort would be purely by ASCII value then you would have to treat the list elements as strings ...
  1. # convert all list items to type string ...
  2. mixed_list = [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789]
  3. str_list = [str(x) for x in mixed_list]
  4. # now it's a pure ASCII sort ...
  5. print sorted(str_list)
  6. """
  7. output -->
  8. ['11', '12', '13456789', '7', '700', '777', '9', 'Dick', 'Mary', 'Paul', 'Zoe']
  9. """
I think that was what Sneekula originally though might have happened.

I leaned on mawe's idea and created a generic function to extract the min and max values of a given type from a mixed list ...
  1. def minmax4(lst, typ):
  2. """return min/max of mixed list lst for items of type typ"""
  3. temp = [x for x in lst if isinstance(x, typ)]
  4. return min(temp), max(temp)
  5.  
  6. # minmax4() test ...
  7. mixed_list = [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789]
  8. print mixed_list
  9.  
  10. print "The min and max of the integers are:"
  11. mn, mx = minmax4(mixed_list, int)
  12. print "minimum = %s maximum = %s" % (mn, mx)
  13.  
  14. print "The min and max of the strings are:"
  15. mn, mx = minmax4(mixed_list, str)
  16. print "minimum = %s maximum = %s" % (mn, mx)
  17. """
  18. minmax4() test output -->
  19. [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789]
  20. The min and max of the integers are:
  21. minimum = 7 maximum = 13456789
  22. The min and max of the strings are:
  23. minimum = Dick maximum = Zoe
  24. """
Last edited by vegaseat; Jan 12th, 2007 at 3:32 am. Reason: lines
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC