[Tutor] get the mode of a list

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Oct 21 21:39:29 CEST 2004



On Thu, 21 Oct 2004, Nick Lunt wrote:

> I really have been putting off posting this to the list cos Im sure
> there's an easy way to do it, but I give up... :(
>
> I want to return the mode of a list, just to clarify the mode is the
> most frequently occurring member of a list in my case.

Hi Nick,


Here's a more general problem that might be easier to solve: can you write
a function that generates a "histogram"?  That is, can you get the
frequencies of all the elements in a list?


For example, something like:

###
>>> histogram([10, 10, 20, 5, 6, 7])
{ 10 : 2,
  20 : 2,
  5 : 1,
  6 : 1,
  7 : 1}
###

If you have something that can generate histogram dictionaries, then you
can use it to solve for modes with relative ease, I think.


Good luck!



More information about the Tutor mailing list