Modal value of an array

Paddy paddy3118 at googlemail.com
Sat Mar 31 01:22:37 EDT 2007


On Mar 30, 10:17 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Thu, 29 Mar 2007 19:44:56 -0300, Paddy <paddy3... at googlemail.com>
> escribió:
>
> > On Mar 29, 8:49 am, a... at mac.com (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 entries by keeping a cache
>
> >>>> foo = ["spam", "eggs", "spam", "spam", "spam", "beans", "eggs"]
> >>>> def cachecount(x, cache={}):
> > ...        return cache.setdefault(x, foo.count(x))
>
> Unfortunately it does, because all arguments are evaluated *before* a
> function call, so you gain nothing.
>
> --
> Gabriel Genellina

I had to experiment to find out what you meant but I finally got it.
that call to foo.count in the setdefault is *always* called. Forgive
my senility.

- Paddy.




More information about the Python-list mailing list