0

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

1 Answers1

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