a_list.count(a_callable) ?

Wildemar Wildenburger wildemar at freakmail.de
Sat Jun 16 13:04:38 EDT 2007


Ping wrote:
> On 6 16 ,   2 06 , "BJörn Lindqvist" <bjou... at gmail.com> wrote:
>   
>> Maybe you could extend count() analogous to how sort() works:
>>     
>
> Wow! This jumps out of my screen!  I like it very much.
> How to get the extension into the language?
>   
Well, you subclass list and extend/override the method.

class SmartCountingList(list):
    def count(self, item, func=lambda x: x):
        return len([item for item in self if func(item) is True])

... or whatever (this is dummy code, not tested and probably riddled 
with stupid errors --- so take this as a pseudocode example)

/W




More information about the Python-list mailing list