944,171 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 14987
  • Python RSS
Jan 10th, 2007
0

min/max of a mixed type list

Expand 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?
Similar Threads
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jan 10th, 2007
0

Re: min/max of a mixed type list

Click to Expand / Collapse  Quote originally posted by sneekula ...
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.
Reputation Points: 741
Solved Threads: 694
Nearly a Posting Maven
woooee is offline Offline
2,314 posts
since Dec 2006
Jan 11th, 2007
0

Re: min/max of a mixed type list

Quote 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:
Quote 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:
Quote 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.
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Jan 11th, 2007
0

Re: min/max of a mixed type list

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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jan 11th, 2007
0

Re: min/max of a mixed type list

Well, here's the first thing that came to my mind:
Python Syntax (Toggle Plain Text)
  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 ...
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Jan 11th, 2007
0

Re: min/max of a mixed type list

Quote ...
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.
Reputation Points: 741
Solved Threads: 694
Nearly a Posting Maven
woooee is offline Offline
2,314 posts
since Dec 2006
Jan 12th, 2007
0

Re: min/max of a mixed type list

Click to Expand / Collapse  Quote originally posted by sneekula ...
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...
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Jan 12th, 2007
0

Re: min/max of a mixed type list

If a sort would be purely by ASCII value then you would have to treat the list elements as strings ...
python Syntax (Toggle Plain Text)
  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 ...
python Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Nested List Searches
Next Thread in Python Forum Timeline: Student GPA





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC