| | |
min/max of a mixed type list
Thread Solved |
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?
[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.
•
•
Join Date: Dec 2006
Posts: 1,000
Reputation:
Solved Threads: 283
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.
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
•
•
•
•
Originally Posted by sneekula
I took a mixed type list and set out to find the min and max values.
).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.
•
•
•
•
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.
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:
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.
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
Well, here's the first thing that came to my mind:
It works, but there must be something more clever. I'll think about it ...
Python Syntax (Toggle Plain Text)
In [17]: lst = [5, "Paul", 4, 2, "Mary", 1, "Dick", "Zoe", 3] In [18]: min_num = min( i for i in lst if isinstance(i, int) ) In [19]: min_str = min( i for i in lst if isinstance(i, str) ) In [20]: min_num Out[20]: 1 In [21]: min_str Out[21]: 'Dick'
•
•
Join Date: Dec 2006
Posts: 1,000
Reputation:
Solved Threads: 283
•
•
•
•
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.
Last edited by woooee; Jan 11th, 2007 at 11:22 pm.
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
•
•
•
•
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?
Python Syntax (Toggle Plain Text)
lst = [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789] slist = sorted(lst) #sort the list min_num , max_str = slist[0], slist[-1] #first and last element usually is minnum, max str. for num,item in enumerate(slist): try: if item.isalpha(): #check for first occurence of a word. min_str = item max_num = slist[num-1] #get the number on the left of this word. Usually is max num break except: pass print min_num ,max_num , max_str , min_str
If a sort would be purely by ASCII value then you would have to treat the list elements as strings ...
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)
# convert all list items to type string ... mixed_list = [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789] str_list = [str(x) for x in mixed_list] # now it's a pure ASCII sort ... print sorted(str_list) """ output --> ['11', '12', '13456789', '7', '700', '777', '9', 'Dick', 'Mary', 'Paul', 'Zoe'] """
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)
def minmax4(lst, typ): """return min/max of mixed list lst for items of type typ""" temp = [x for x in lst if isinstance(x, typ)] return min(temp), max(temp) # minmax4() test ... mixed_list = [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789] print mixed_list print "The min and max of the integers are:" mn, mx = minmax4(mixed_list, int) print "minimum = %s maximum = %s" % (mn, mx) print "The min and max of the strings are:" mn, mx = minmax4(mixed_list, str) print "minimum = %s maximum = %s" % (mn, mx) """ minmax4() test output --> [11, 'Dick', 12, 'Mary', 7, 'Zoe', 9, 700, 777, 'Paul', 13456789] The min and max of the integers are: minimum = 7 maximum = 13456789 The min and max of the strings are: minimum = Dick maximum = Zoe """
Last edited by vegaseat; Jan 12th, 2007 at 3:32 am. Reason: lines
May 'the Google' be with you!
![]() |
Similar Threads
- Starting Python (Python)
- min and max values (C++)
Other Threads in the Python Forum
- Previous Thread: Nested List Searches
- Next Thread: Student GPA
| Thread Tools | Search this Thread |
alarm ansi assignment avogadro backend beginner binary bluetooth character cipher cmd customdialog cx-freeze data decimals dictionary directory dynamic error exe file float format function generator getvalue gnu graphics halp heads homework http ideas import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path pointer port prime programming progressbar push py2exe pygame python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh statistics string strings sudokusolver sum text thread threading time tlapse tuple tutorial ubuntu unicode urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia write wxpython xlib






