By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,842 Members | 2,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,842 IT Pros & Developers. It's quick & easy.

finding the biggest item in a list

P: 54
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
Share this Question
Share on Google+
6 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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
Expert 5K+
P: 6,596
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

P: 54
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
Expert 5K+
P: 6,596
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

Expert 100+
P: 511
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
Expert 5K+
P: 6,596
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

Post your reply

Sign in to post your reply or Sign up for a free account.