I am trying to write a simple little program that calculates the mean, median, mode and standard deviation of a given set of numbers, but I am having trouble writting the script for the mode part, any help? I am using python 3.3.2
Asked
Active
Viewed 93 times
0
-
2What have you tried so far? We can help you out when you get stuck, but we won't do all of the work for you. :) – Blue Ice Mar 20 '14 at 21:47
-
do you want to see the entire script or that section in particular? – HaloEvent Mar 21 '14 at 02:40
1 Answers
1
Try the Counter module:
import collections
c = collections.Counter('extremely')
c
Out[4]: Counter({'e': 3, 'm': 1, 'l': 1, 'r': 1, 't': 1, 'y': 1, 'x': 1})
c.items()
Out[7]: [('e', 3), ('m', 1), ('l', 1), ('r', 1), ('t', 1), ('y', 1), ('x', 1)]
srted = sorted(c.items(), key= lambda (k,v): -v)
srted
Out[9]: [('e', 3), ('m', 1), ('l', 1), ('r', 1), ('t', 1), ('y', 1), ('x', 1)]
top = srted[0]
top
Out[11]: ('e', 3)
k,v = top
k
Out[13]: 'e'
v
Out[14]: 3

Simon
- 2,840
- 2
- 18
- 26
-
i.e. pass counter some sequence\iterable, in this case I used the string "extremely" as an iterable of chars. – Simon Mar 20 '14 at 22:07