[Tutor] same ol s

Brian van den Broek bvande at po-box.mcgill.ca
Thu Aug 12 01:10:42 CEST 2004


jason hochstein said unto the world upon 2004-08-11 18:19:
> I am still having problems getting the median and mode of a list. Any
> ideas?? I am confused on how you tell the code to decipher from a list
> with an even or odd amount of numbers in it. The mode is totally
> eluding me.

Hi Jason,

it would be easier for the people on the list to help you if you shared 
what you had tried--even if it doesn't do what you want, a code sample 
will help people figure out how to formulate a response so that it can 
help you solve the problem, rather than solving it for you. And don't 
worry about feeling that its not "good enough" -- I've tried my best is 
posting silly learner things and haven't yet made a single tutor member 
ridicule me ;-)

I'll address the mode part of your question. I'm going to give it more in 
English than code. My suggestion would be to try to put something like 
what I here describe into code and to post another question if you get 
stuck on how to do that. I've purposely been silent on a few parts of the 
dictionary techniques I mention. See section 2.3.7 Mapping Types of the 
library reference for the exact syntax details.

Let's assume you have a list of integers and you want to find the mode or 
modes of that list. And, just to be explicit, by "mode" I mean the most 
frequently occurring data point in the list. So, for example [1, 2, 2, 3, 
4, 4, 4, 5] would have 4 as its mode. There may not be a unique mode: [1, 
1, 2, 3, 3] has both 1 and 3 occurring most often.

Now what's the mode of [1, 4, 5, 2, 3, 3, 4, 5, 6, 5, 6]? 5. How do you 
tell? Well, if you are like me and find this list too long to take in with 
a single glance, you go through the list, keeping a running count of how 
often each data point occurs, and on getting to the end, see that there 
were more 5's than anything else.

That suggests a strategy for doing it in Python. Iterate through the list, 
keeping count of how often each data point has appeared.

The way I'd do it (no bets that this is best; I'm learning too) is to use 
a dictionary. Integers can be used a dictionary keys (this is because they 
are immutable--though there might be more to it than just that). So, I'd 
initialize an empty dictionary before I start my loop:

occurrence_count = {}

Then, I'd iterate through my list, considering each data point one by one. 
You'd do this like so:

for i in integer_list:   # where integer_list is the collection of data
	# Fancy code logic here

What you would then do in the indented fancy code logic block is check for 
whether occurrence_count had i as a key. (i will take each of the values 
in your list as the iteration repeats again and again going through the 
list, so this is really checking of each data point whether it is a key in 
your dictionary.) If it doesn't, this is because it is the first time 
you've seen that number. So, make a new key/value pair for your dictionary 
with i as key and value as 1 (its the first occurrence after all):

occurrence_count[i] = 1

If it does have a key, you need to augment the key by 1. So:

occurrence_count[i] = occurrence_count[i] + 1

Once the iteration over the list is done, you'll have a dictionary where 
every element in your data is a key, and the values will tell you how 
often that data element occurred. From that you can have Python tell you 
which element occurred most often. I'll leave that to you entirely. Have a 
try, and ask again with some code showing what you tried and others on the 
list will surely be happy to help you some more.

I hope that helps get you started. Best,

Brian vdB


More information about the Tutor mailing list