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

4

1 回答 1

1

试试计数器模块:

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

输出[11]: ('e', 3)

k,v = top
k

出[13]:'e'

v

出[14]:3

于 2014-03-20T22:07:04.410 回答