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
6 1279 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
- >>> numbers = ['4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8']
-
>>> max(numbers)
-
'8'
-
>>> max([float(n) for n in numbers])
-
20.0
-
>>>
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
-
>>> numbers = ['4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8']
-
>>> floatNums = [float(x) for x in numbers]
-
>>> max(floatNums)
-
20.0
-
>>> # but that won't work on strings #
-
>>> max(numbers)
-
'8'
-
>>>
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. -
>>>import re
-
>>> stuff
-
'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'
-
>>> stuff2 = re.split('\n', prices)
-
>>> stuff2
-
['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', '']
-
>>> floatStuff = [float(i) for i in stuff2]
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in <module>
-
ValueError: empty string for float()
-
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. -
>>>import re
-
>>> stuff
-
'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'
-
>>> stuff2 = re.split('\n', prices)
-
>>> stuff2
-
['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', '']
-
>>> floatStuff = [float(i) for i in stuff2]
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in <module>
-
ValueError: empty string for float()
-
Line #10 says it all. """ValueError: empty string for float()"""
Since it appears to be the very last entry only, you could do: -
>>> floatStuff = [float(i) for i in stuff2[:-1]] # "slice" the last element off
or - >>> 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) - >>> # use the "predicate" part of the list comprehension.
-
>>> floatStuff = [float(floatStr) for floatStr in stuff2 if floatStr]
i don't know why you are using re. however, you should always go for the basics if its available -
>>> stuff.strip().split()
-
i don't know why you are using re. however, you should always go for the basics if its available -
>>> stuff.strip().split()
-
A very elegant solution IFF the values in the file can be guaranteed to be valid floats.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
3 posts
views
Thread by Noam Dekers |
last post: by
|
1 post
views
Thread by Phil Watkins |
last post: by
|
3 posts
views
Thread by KL |
last post: by
|
11 posts
views
Thread by paradox |
last post: by
|
2 posts
views
Thread by ElkGroveR |
last post: by
|
1 post
views
Thread by Simon Forman |
last post: by
|
3 posts
views
Thread by garyusenet |
last post: by
|
5 posts
views
Thread by davenet |
last post: by
|
22 posts
views
Thread by Simon Forman |
last post: by
| | | | | | | | | | |