[Tutor] toggle through a list CORRECTION

bob gailer bgailer at gmail.com
Thu Apr 2 03:54:54 CEST 2009


bob gailer wrote:
> 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 num in data:  # correction
>        numdict[num] += 1   # correction
>    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