Modal value of an array
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Fri Mar 30 17:17:41 EDT 2007
En Thu, 29 Mar 2007 19:44:56 -0300, Paddy <paddy3118 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
More information about the Python-list
mailing list