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 1368 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. """ValueErr or: 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 named
textOrigin.txt in which I store the text to search in.
I would like my prog to check if a certain word appears in the text
and than to tell me what line it found it in (if it did...).
My problem is that the script can't find the words I'm...
|
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 far seems to indicate the use of
the "contains" command but I cannot find a helpfull
reference.
Any help greatly apreciated as I am workin on the second
bottle of nurophen
|
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 after that spot is found.
Well, I am kinda lost on this finding things in a vector. I mean,
wouldn't you just go to vector, and then ....well then I gotta find
out how to do an insert in a vector. Does this involve changing the
pointer to point...
|
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 example, if the ArrayList has the following elements:
elems = "blue"
elems = "red"
elems = "blue"
elems = "green"
|
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.
The SELECT query is built as follows:
$itemtype = stripslashes(trim($_POST));
| |
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
item. (Actually, it's a generator, and I use the set class outside of
it to collect the unique items, but you get the idea. ;-) )
data = ,
,
,
|
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 programmes features is that it displays a list. The contents
of this list are the names of people that are logged into the
programme.
|
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
contained in this group of objects.
The objects are defined as follows:
|
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: 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |