473,378 Members | 1,413 Online

# finding the biggest item in a list

lets say i have this list:
numbers = ['4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8']

and i want my script to tell me that the highest number in the list is '20'

how can i do that?

thnx
Aug 31 '07 #1
6 1359
bvdet
2,851 Expert Mod 2GB
lets say i have this list:
numbers = ['4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8']

and i want my script to tell me that the highest number in the list is '20'

how can i do that?

thnx
Expand|Select|Wrap|Line Numbers
1. >>> numbers = ['4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8']
2. >>> max(numbers)
3. '8'
4. >>> max([float(n) for n in numbers])
5. 20.0
6. >>>
Aug 31 '07 #2
bartonc
6,596 Expert 4TB
lets say i have this list:
numbers = ['4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8']

and i want my script to tell me that the highest number in the list is '20'

how can i do that?

thnx
Expand|Select|Wrap|Line Numbers
1. >>> numbers = ['4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8']
2. >>> floatNums = [float(x) for x in numbers]
3. >>> max(floatNums)
4. 20.0
5. >>> # but that won't work on strings #
6. >>> max(numbers)
7. '8'
8. >>>
Aug 31 '07 #3
what's the difference from what you're doing in what i'm trying below. i'll tell you one difference: what you do works, and what I'm trying doesn't.
Expand|Select|Wrap|Line Numbers
1. >>>import re
2. >>> stuff
3. '37.04\n36.55\n35.05\n35.25\n34.68\n34.02\n34.77\n34.3\n33.79\n33.31\n32.82\n33.36\n33.52\n35.24\n35.92\n35.79\n37.8\n36.4\n36.89\n36.19\n37.82\n37.77\n37.17\n37.81\n36.75\n37.3\n38.04\n37.95\n38.96\n39.39\n39.7\n40.04\n40.15\n40.58\n40.87\n40.94\n40.48\n40.25\n40.23\n40.3\n39.83\n39.2\n39.37\n39.35\n39.52\n39.76\n39.15\n39.18\n39.36\n39.95\n40.03\n38.27\n37.96\n37.95\n37.79\n37.73\n37.36\n37.71\n37.95\n37.66\n38.3\n38.86'
4. >>> stuff2 = re.split('\n', prices)
5. >>> stuff2
6. ['37.04', '36.55', '35.05', '35.25', '34.68', '34.02', '34.77', '34.3', '33.79', '33.31', '32.82', '33.36', '33.52', '35.24', '35.92', '35.79', '37.8', '36.4', '36.89', '36.19', '37.82', '37.77', '37.17', '37.81', '36.75', '37.3', '38.04', '37.95', '38.96', '39.39', '39.7', '40.04', '40.15', '40.58', '40.87', '40.94', '40.48', '40.25', '40.23', '40.3', '39.83', '39.2', '39.37', '39.35', '39.52', '39.76', '39.15', '39.18', '39.36', '39.95', '40.03', '38.27', '37.96', '37.95', '37.79', '37.73', '37.36', '37.71', '37.95', '37.66', '38.3', '38.86', '']
7. >>> floatStuff = [float(i) for i in stuff2]
8. Traceback (most recent call last):
9.   File "<interactive input>", line 1, in <module>
10. ValueError: empty string for float()
11.
Aug 31 '07 #4
bartonc
6,596 Expert 4TB
what's the difference from what you're doing in what i'm trying below. i'll tell you one difference: what you do works, and what I'm trying doesn't.
Expand|Select|Wrap|Line Numbers
1. >>>import re
2. >>> stuff
3. '37.04\n36.55\n35.05\n35.25\n34.68\n34.02\n34.77\n34.3\n33.79\n33.31\n32.82\n33.36\n33.52\n35.24\n35.92\n35.79\n37.8\n36.4\n36.89\n36.19\n37.82\n37.77\n37.17\n37.81\n36.75\n37.3\n38.04\n37.95\n38.96\n39.39\n39.7\n40.04\n40.15\n40.58\n40.87\n40.94\n40.48\n40.25\n40.23\n40.3\n39.83\n39.2\n39.37\n39.35\n39.52\n39.76\n39.15\n39.18\n39.36\n39.95\n40.03\n38.27\n37.96\n37.95\n37.79\n37.73\n37.36\n37.71\n37.95\n37.66\n38.3\n38.86'
4. >>> stuff2 = re.split('\n', prices)
5. >>> stuff2
6. ['37.04', '36.55', '35.05', '35.25', '34.68', '34.02', '34.77', '34.3', '33.79', '33.31', '32.82', '33.36', '33.52', '35.24', '35.92', '35.79', '37.8', '36.4', '36.89', '36.19', '37.82', '37.77', '37.17', '37.81', '36.75', '37.3', '38.04', '37.95', '38.96', '39.39', '39.7', '40.04', '40.15', '40.58', '40.87', '40.94', '40.48', '40.25', '40.23', '40.3', '39.83', '39.2', '39.37', '39.35', '39.52', '39.76', '39.15', '39.18', '39.36', '39.95', '40.03', '38.27', '37.96', '37.95', '37.79', '37.73', '37.36', '37.71', '37.95', '37.66', '38.3', '38.86', '']
7. >>> floatStuff = [float(i) for i in stuff2]
8. Traceback (most recent call last):
9.   File "<interactive input>", line 1, in <module>
10. ValueError: empty string for float()
11.
Line #10 says it all. """ValueError: empty string for float()"""
Since it appears to be the very last entry only, you could do:
Expand|Select|Wrap|Line Numbers
1. >>> floatStuff = [float(i) for i in stuff2[:-1]]   # "slice" the last element off
or
Expand|Select|Wrap|Line Numbers
1. >>> stuff2 = re.split('\n', prices)[:-1]   # "slice" the last element off
or (but I haven't tested this. If may evaluate the float() before the if)
Expand|Select|Wrap|Line Numbers
1. >>> # use the "predicate" part of the list comprehension.
2. >>> floatStuff = [float(floatStr) for floatStr in stuff2 if floatStr]
Sep 1 '07 #5
ghostdog74
511 Expert 256MB
i don't know why you are using re. however, you should always go for the basics if its available
Expand|Select|Wrap|Line Numbers
1. >>> stuff.strip().split()
2.
Sep 1 '07 #6
bartonc
6,596 Expert 4TB
i don't know why you are using re. however, you should always go for the basics if its available
Expand|Select|Wrap|Line Numbers
1. >>> stuff.strip().split()
2.
A very elegant solution IFF the values in the file can be guaranteed to be valid floats.
Sep 1 '07 #7