473,499 Members | 1,710 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 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
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. """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
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
13765
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...
1
6463
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...
3
2213
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...
11
33450
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...
2
2835
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. ...
1
1674
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...
3
1559
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...
5
6307
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...
22
6863
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
7132
marktang
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,...
0
7009
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...
0
7178
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,...
0
7223
jinu1996
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...
1
6899
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...
0
5475
agi2029
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,...
0
4602
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...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.