473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to access the middle value of a list separated by commas

[3,4,5]

and i want to assign these values to variables, but i want to assign the number with the middle VALUE, how do i go about doing that?
Oct 20 '10 #1
8 2717
dwblas
626 Expert 512MB
See Section 1.1.5 at this onine book.
Oct 20 '10 #2
that didn't really help :/... if i already assigned 2 of the numbers can i assign it by the only unassigned element in the list?
Oct 20 '10 #3
dwblas
626 Expert 512MB
You should at least read it, before asking someone else to do it for you. Verbatim copy:
l = ['a', 'b', 1, 'a cat']
and access it with indices, exactly as you did with character strings:
>>> l[1]
'b'
Oct 20 '10 #4
yes yes, i know that but the problem was that the list is user inputed and it is subject to change so that wont work and besides i read it all, it doesn't even cover all the function possibilities of a list.
Oct 20 '10 #5
Glenton
391 Expert 256MB
Do you mean the median of the list? Eg if your list were [5,3,4] would you want to assign a variable to 4, or to 3? I think the question is ambiguous. Perhaps you can try to be more clear about exactly what it is that you want.

Is the issue that the list might have a variable number of elements? Or that you want to assign to the median element? Or that you want to assign to an element that has not yet been used for something?

Help us to help you!
Oct 25 '10 #6
yes,It was a question about returning the median of a list of only 3 elements that the user enters, heres a snippet of my code if it helps, i need B to represent the median of the list.

[side_1=input('Enter side one: ')
side_2=input('Enter side two: ')
side_3=input('Enter side three: ')
values=[side_1,side_2,side_3]
A=min(values)
B=
C=max(values)]
Oct 25 '10 #7
bvdet
2,851 Expert Mod 2GB
The median of any list is the middle value in a sorted list.
Expand|Select|Wrap|Line Numbers
  1. >>> values = [3,8,2,9,6]
  2. >>> sorted(values)
  3. [2, 3, 6, 8, 9]
  4. >>> sorted(values)[len(values)/2]
  5. 6
  6. >>> 
It's a bit more complicated when the list contains an even number of elements.
Oct 25 '10 #8
Glenton
391 Expert 256MB
bvdet's method should work fine for you, Justin. Although, if you're using python 3, if I recall it won't do the floor division, so instead use:
Expand|Select|Wrap|Line Numbers
  1. B=sorted(values)[(len(values)-1)/2]
or, since you know the length already, just use:
Expand|Select|Wrap|Line Numbers
  1. B=sorted(values)[1]
Oct 26 '10 #9

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

Similar topics

12
by: Brett L. Moore | last post by:
Hi, I have had trouble determining whether the STL list.size() operation is O(1) or O(n). I know the list is a doubly-linked list, so if the size() operation begins at the head, then counts to...
30
by: Steven Bethard | last post by:
George Sakkis wrote: > "Steven Bethard" <steven.bethard@gmail.com> wrote: >> Dict comprehensions were recently rejected: >> http://www.python.org/peps/pep-0274.html >> The reason, of course,...
1
by: Mukesh | last post by:
Hi all, First of all thank u, It is possible to get a list of ip addresses in a len if yes then how and what are the different libs for diffrent os like linux windows.
2
by: c_kubie | last post by:
I am not very Access03 efficient. I have managed to create a listbox within a form. I would like to have the listbox selection update the form with the appropriate information from that row. ...
1
by: Ron | last post by:
Hi, I need to prefome a sync process on two Lists Lets say i have two Lists of type string. And each list can be dimensioned differently, but will eventually contain the same amount of elements...
1
by: Weste | last post by:
I have 2 list boxes bound to a database. The 1st list box displays services ordered by the customer. I want the 2nd list box to display services not yet ordered by the customer. For example. My...
10
by: zfareed | last post by:
I have a program that creates a linked list and is required to count the number of elements in the list. This is what I have thus far. #include <iostream> using namespace std; struct listrec {...
4
by: J2EE Convert | last post by:
Hello, Sorry if this has already been beaten to death. However, I was not able to find the answer in my searches. Is there any performance trade off between creating and using a generic list of...
4
by: parez | last post by:
Hi, I am trying to serialize List<List<string>. With the following code public List<List<string>DataRows { get; set; }
10
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.