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 1361 bvdet 2,851
Recognized Expert Moderator Specialist
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
-
>>>
bartonc 6,596
Recognized Expert Expert
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()
-
bartonc 6,596
Recognized Expert Expert
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()
-
bartonc 6,596
Recognized Expert Expert
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Noam Dekers |
last post by:
Hi all,
I would like to find a word stored in a text file.
Structure: I have one file named keyWords.txt that stores some key
words I'm interested in finding. In addition I also have a file...
|
by: Phil Watkins |
last post by:
I am a novice programer in Vb and I am having a major
brain ache finding out which item has been selected within
a list view and then either deleting that item or editing
them. My searching so...
|
by: KL |
last post by:
Well, I am back.
This time our assignment has us filling a vector and then timing how
long it takes to find a spot in the vector to insert a new item, and
the time required to insert the item...
|
by: paradox |
last post by:
Basically I have an ArrayList of strings; I need a fast way of
searching through and comparing the elements of the ArrayList and the
return an ArrayList of items that have 3 Duplicates.
For...
|
by: ElkGroveR |
last post by:
Hi there!
I'm using PHP to create a simple, dynamic MySQL SELECT query.
The user chooses a selection from a HTML Form SELECT element's many options
and submits the form via a POST action.
...
| |
by: Simon Forman |
last post by:
I've got a function that I'd like to improve.
It takes a list of lists and a "target" element, and it returns the set
of the items in the lists that appear either before or after the target...
|
by: garyusenet |
last post by:
Some time ago I enquired about how I interface with a program written
in an old version of C++
Any terms i use like list that follow are used in their common everyday
usuage!
One of the...
|
by: davenet |
last post by:
Hi,
I'm new to Python and working on a school assignment.
I have setup a dictionary where the keys point to an object. Each
object has two member variables. I need to find the smallest value...
|
by: Simon Forman |
last post by:
Is there a more efficient way to do this?
def f(L):
'''Return a set of the items that occur more than once in L.'''
L = list(L)
for item in set(L):
L.remove(item)
return set(L)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |