a_list.count(a_callable) ?

Dustan DustanGroups at gmail.com
Sat Jun 16 22:37:01 CEST 2007


On Jun 16, 12:04 pm, Wildemar Wildenburger <wilde... at freakmail.de>
wrote:
> class SmartCountingList(list):
>     def count(self, item, func=lambda x: x):
>         return len([item for item in self if func(item) is True])

A less bug-prone and (I would think) speedier example, although still
untested:

class SmartCountingList(list):
    def count(self, item, func=lambda x: x):
        return sum(1 for i in self if func(item)==item)

Then, you would call it as follows:
a_list.count(True, a_function)




More information about the Python-list mailing list