Re: Modal value of an array

2007-03-30 Thread Paddy
On Mar 30, 10:17 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Thu, 29 Mar 2007 19:44:56 -0300, Paddy <[EMAIL PROTECTED]> > escribió: > > > On Mar 29, 8:49 am, [EMAIL PROTECTED] (Alex Martelli) wrote: > >> >>> foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"] > >> >>> max(fo

Re: Modal value of an array

2007-03-30 Thread Gabriel Genellina
En Thu, 29 Mar 2007 19:44:56 -0300, Paddy <[EMAIL PROTECTED]> escribió: > On Mar 29, 8:49 am, [EMAIL PROTECTED] (Alex Martelli) wrote: >> >>> foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"] >> >>> max(foo, key=foo.count) >> >> 'spam' > > This doesn't call foo.count for duplicate

Re: Modal value of an array

2007-03-29 Thread Paddy
On Mar 30, 2:58 am, [EMAIL PROTECTED] (Alex Martelli) wrote: > Paddy <[EMAIL PROTECTED]> wrote: > >... > > > > > > A bit more directly: > > > > >>> foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"] > > > >>> max(foo, key=foo.count) > > > > 'spam' > > > > Alex > > > This doesn't ca

Re: Modal value of an array

2007-03-29 Thread Alex Martelli
Paddy <[EMAIL PROTECTED]> wrote: ... > > A bit more directly: > > > > >>> foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"] > > >>> max(foo, key=foo.count) > > > > 'spam' > > > > Alex > > This doesn't call foo.count for duplicate entries by keeping a cache > > >>> foo = ["spam",

Re: Modal value of an array

2007-03-29 Thread Paddy
On Mar 29, 8:49 am, [EMAIL PROTECTED] (Alex Martelli) wrote: > Ben Finney <[EMAIL PROTECTED]> wrote: > >... > > > That's not the only case though. What do you expect to be returned for > > an input of ["eggs", "beans", "beans", "eggs", "spam"] ? > > > Assuming you want *a* mode value, and any o

Re: Modal value of an array

2007-03-29 Thread bearophileHUGS
Alex Martelli: > >>> foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"] > >>> max(foo, key=foo.count) It's a very nice solution, the shortest too. But I think it's better to develop your own well tested and efficient stats module (and there is one already done that can be found around

Re: Modal value of an array

2007-03-28 Thread Alex Martelli
Ben Finney <[EMAIL PROTECTED]> wrote: ... > That's not the only case though. What do you expect to be returned for > an input of ["eggs", "beans", "beans", "eggs", "spam"] ? > > Assuming you want *a* mode value, and any one will do (e.g. any of > "spam", "eggs" or "beans" is okay), I'd write it

Re: Modal value of an array

2007-03-28 Thread Paddy
On Mar 29, 4:40 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Hi > > How can I find out the modal value in an array. That is the value > which occurs maximum time in the sequence .. > > e.g. if my array has values like [2,3,2,2,2,4,2,2] definitely the > maximum time 2 occurs in the array.

Re: Modal value of an array

2007-03-28 Thread Steven D'Aprano
On Wed, 28 Mar 2007 20:40:22 -0700, [EMAIL PROTECTED] wrote: > Hi > > How can I find out the modal value in an array. That is the value > which occurs maximum time in the sequence .. > > e.g. if my array has values like [2,3,2,2,2,4,2,2] definitely the > maximum time 2 occurs in the array. so

Re: Modal value of an array

2007-03-28 Thread Ben Finney
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > Hi > > How can I find out the modal value in an array. That is the value > which occurs maximum time in the sequence .. > > e.g. if my array has values like [2,3,2,2,2,4,2,2] definitely the > maximum time 2 occurs in the array. so this function s