[Tutor] toggle through a list
bob gailer
bgailer at gmail.com
Thu Apr 2 03:28:13 CEST 2009
j mercedes wrote:
> Hi, I have problem here...I am writing a program but I am getting
> stucked at how to solve the mode of a series of numbers...any ideas???
The customary approach is to collect counts of each value in a
dictionary. Untested code follows:
from collections import defaultdict
def mode(data):
numdict = defaultdict(int)
for i in data:
numdict[data[i]] += 1
numlist = [(count, num) for num, count in nums.items()]
numlist.sort()
print 'The mode is: ', numlist[-1][1]
--
Bob Gailer
Chapel Hill NC
919-636-4239
More information about the Tutor
mailing list