473,624 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finding the biggest item in a list

54 New Member
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 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
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 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
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
Patrick C
54 New Member
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 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.
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. """ValueErr or: 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 Recognized Expert Contributor
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 Recognized Expert Expert
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

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

Similar topics

3
13785
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...
1
6487
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
3
2227
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...
11
33490
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"
2
2842
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));
1
1681
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 = , , ,
3
1568
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.
5
6319
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:
22
6906
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)
0
8179
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,...
0
8685
Oralloy
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...
1
8348
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,...
0
8493
tracyyun
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...
1
6112
isladogs
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...
0
4084
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...
0
4187
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1797
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1493
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.