[Tutor] same ol s

Dragonfirebane at aol.com Dragonfirebane at aol.com
Thu Aug 12 02:18:25 CEST 2004


In a message dated 8/11/2004 6:21:25 PM Eastern Standard Time, 
bigapple631 at optonline.net writes:
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.
Well, you can use len(list) to determine if its even or odd:

###
if str(len(list)/2.0)[-1] != 0:
    print 'Odd number of elements.'
else:
    print 'Even number of elements.'
###

Then based on that, sort() the list and get the middle element, like so:

###
print 'Median: %s' % list[int(round(len(list)/2.0))]
###

if the length of the list is odd or like so:

###
print 'Medians: %s, %s' % (list[(len(list)/2) - 1], list[(len(list)/2)])
###

if it is even, to get the median.

As for the mode, the suggestion made by bvande at po-box.mcgill.ca seems 
reasonable.

Email: dragonfirebane at aol.com
AIM: singingxduck
Programming Python for the fun of it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040811/9d1c43c9/attachment.html


More information about the Tutor mailing list