[Tutor] mode?? (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Jul 28 02:24:52 CEST 2004


Hi Jason,


Thanks for the response!  Ok, let me forward your response to Tutor so
that the folks on the list can help.  If your email client allows it, try
to use 'Reply to All', which should include the Tutor list in CC.


One suggestion is to look at how Python lists work: you definitely want to
look at how to work with lists, since you're dealing with a collection of
numbers.  I wouldn't worry about modules yet; they're not hard, but aren't
necessary for your problem.


Lists allow us to use a single container to read in an arbitrary amount of
things.  For example:


###
>>> list_of_numbers = []
>>> print list_of_numbers
[]
>>> list_of_numbers.append(3)
>>> list_of_numbers.append(1)
>>> list_of_numbers.append(4)
>>> list_of_numbers.append(1)
>>> list_of_numbers.append(5)
>>> list_of_numbers
[3, 1, 4, 1, 5]
###

Whenever we want to add a new number at the end of our list, we can
append() it.  Note that we don't need separate variables like
'first_element' or 'second_element' or 'third_element': we can access them
all by using the list 'list_of_elements'.


And getting at an arbitrary element of our list isn't so bad either.  We
"index" an element of our list by using the following bracket notation:

###
>>> list_of_numbers[0]
3
>>> list_of_numbers[1]
1
>>> list_of_numbers[2]
4
###


Lists support a few things that are nice: we can ask how many times an
element occurs in a list by counting it:

###
>>> list_of_numbers.count(1)
2
>>> list_of_numbers.count(42)
0
>>> list_of_numbers.count(3)
1
###


And as one last thing, lists can be "looped" or "iterated" across:

###
>>> for n in list_of_numbers:
...     print n, n*2, n*3
...
3 6 9
1 2 3
4 8 12
1 2 3
5 10 15
###

I'm doing a really fast overview of lists; does this make some sense so
far?


The problem that you're working on will become much easier if you use a
collection.  Trying to do it without a collection is really ugly; it's
doable, but the correct code to do it ends up being one big mess of a
special-case thing... and it'll probably be hardcoded to only deal with
seven elements.  *grin*


Play around with lists first to get some familiarity with them, and then
go back and try solving the statistical problems with them; it should turn
out to be a snap with lists.

You mentioned that you looked at a few tutorials; you may find:

    http://www.ibiblio.org/obp/thinkCSpy/chap08.htm

one of the more useful ones, since it concentrates specifically on how to
play with Python lists.  But the other tutorials on:

    http://www.python.org/topics/learn/non-prog.html

should also have sections on list manipulation that should help.


And again, feel free to ask questions about how to use lists on Tutor;
we'll be happy to help.


Good luck!



---------- Forwarded message ----------
Date: Tue, 27 Jul 2004 18:57:03 -0400
From: jason hochstein <bigapple631 at optonline.net>
To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] mode??

I understand what the meaning of mode is. I also looked at the mathworld
site prior to starting this program. I am at a total loss as to how I would
get my program to output the mode of a group of numbers. The mode being the
most common number in the bunch. Here is what I have so far:

print "This program will give you the Average, Statistical median and Mode
of any 7 numbers."
print "Please enter your numbers in ascending order."
print "Try it if you think I am lying!!"

first = input ("What is your first number? ")
second = input ("What is your second number? ")
third = input ("What is your third number? ")
fourth = input ("What is your fourth number? ")
fifth = input ("What is your fifth number? ")
sixth = input ("What is your sixth number? ")
seventh = input ("What is your seventh number? ")

average = first + second + third + fourth + fifth + sixth + seventh / 7
median = fourth
mode =

print "The average of these numbers is", average
print "The median of this list is", fourth




Python data structures is definitly my problem. Lists and modules and
things of that nature are eluding me for some reason. I can't figure out
how to inport and export either. I am very new to programming and Python
is my first attempt at it. I have been through massive amounts of
tutorials. Any help explaining how some of these things work would be
greatly appreciated. By no means am I trying to get someone to do the work
for me. I want to learn how to do it myself. Thanks again.


----- Original Message -----
From: "Danny Yoo" <dyoo at hkn.eecs.berkeley.edu>
To: "Kent Johnson" <kent_johnson at skillsoft.com>
Cc: <tutor at python.org>
Sent: Monday, July 26, 2004 9:09 PM
Subject: Re: [Tutor] mode??


>
>
> On Mon, 26 Jul 2004, Kent Johnson wrote:
>
> > Here is one way to do it. This will find the mode(s) of a list of any
kind
> > of items, not just numbers.
>
> [answer cut]
>
>
> Hmmmm... next time, it might be best to wait for a bit, and hold off on
> direct answers until we know more.  I don't think it's enough to give
> answers --- we have to find out why Jason had trouble with the problem.
> The root cause for his difficulty is still an unknown.
>
>
> We have no idea if it's because he's misunderstanding the definition of
> 'mode', or if he's having problems with using Python's data structures
> effectively.  From what he said earlier, it sounded like he didn't
> understand what 'mode' meant, but that's only my guess.
>
>
> I guess I'm trying to say: don't do his homework!  *grin*
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list