472,988 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 software developers and data experts.

finding the median item in a list

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?
what if you wanted to find a number in the middle of that list: how would you go about that?
Sep 8 '07 #1
2 16550
bartonc
6,596 Expert 4TB
what if you wanted to find a number in the middle of that list: how would you go about that?
Here's one way:
Expand|Select|Wrap|Line Numbers
  1. >>> numbers = ('4', '3.2', '3.1', '3.15', '20', '3', '4', '17', '8')
  2. >>> floatList = sorted([float(x) for x in numbers])
  3. >>> floatList
  4. [3.0, 3.1000000000000001, 3.1499999999999999, 3.2000000000000002, 4.0, 4.0, 8.0, 17.0, 20.0]
  5. >>> average = sum(floatList)//len(floatList)
  6. >>> average
  7. 7.0
  8. >>> for i, item in enumerate(floatList):
  9. ...     if item > average:
  10. ...         print "%f is between %f and %f" %(average, item, floatList[i + 1])
  11. ...         break 
  12. 7.000000 is between 8.000000 and 17.000000
  13. >>> 
Sep 8 '07 #2
bartonc
6,596 Expert 4TB
what if you wanted to find a number in the middle of that list: how would you go about that?
Or, if all you want is the number closest in position to the middle, then it's:
Expand|Select|Wrap|Line Numbers
  1. >>> print floatList[len(floatList)/2]
  2. 4.0
  3. >>> 
Sep 8 '07 #3

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

Similar topics

2
by: Bob | last post by:
I have been looking at the code for MedianFind(pDte As String) from the following thread from UtterAccess.com: "Finding Median average grouped by field" I have been able to get it to run using...
8
by: nick.vitone | last post by:
Hi, I'm somewhat of a novice at Access, and I have no experience programming whatsoever. I'm attempting to calculate the statistical median in a query. I need to "Group by" one column and find...
2
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. ...
12
by: erikcw | last post by:
Hi all, I have a collection of ordered numerical data in a list. The numbers when plotted on a line chart make a low-high-low-high-high-low (random) pattern. I need an algorithm to extract the...
3
by: Storm9 | last post by:
I have to: "Use functional decomposition to write a C++ program that determines the median of five input numbers. The median is the middle number when the five are arranged in order. However, the...
7
by: Bhadan | last post by:
Hello, I have several jagged arrays which have been sorted. I'm trying to find the median of each array. Any tips appreciated. TIA. Bhads.
3
by: mehwishobaid | last post by:
i dont know wat is wrong with my code. when i compile. i get the error saying line 29: error: expression must have pointer-to-object type #include <iostream> using namespace std; #include...
7
by: tomshorts07 | last post by:
hi everyone! i was wondering if anyone had any ideas on how to go about coding a method that would determine the median of a list of numbers just to clarify- a median is the 'middle value' when...
2
by: curien | last post by:
Hello, I am working on a code that takes the users input ( a string of digits) from the command line and prints out the median number. I am trying to accomplish this by: from sys import argv ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.