a_list.count(a_callable) ?

Ping ping.nsr.yeh at gmail.com
Sun Jun 17 11:42:47 EDT 2007


Somehow I did not see my post sent about 10 hours ago.
I'm posting it again.  I apologize if it showed up twice.

After seeing all the ideas tossed around, now I like
the proposal made by BJörn Lindqvist the best, and I
extend it further to match the sort() method:
   L.count(value, cmp=None, key=None)
With this signature, the callable case is simply
   L.count(True, cmp=a_callable),
although now a_callable must return True instead
of anything logical equivalent.  I can live with that.

I made an implementation with subclassing and
Carsten Haese's sum(1 ...) method, see below.
It works fine for me.  It would be great to see
it supported by the built-in list.  :)

cheers,
Ping

$ cat slist.py
#!/usr/bin/env python

from operator import *

class slist (list):
        def count(self, value, cmp=None, key=None):
                if not cmp and not key: return list.count(self, value)
                if not cmp:     cmp = eq
                if not key:     # cmp given, no key
                        return sum(1 for i in self if cmp(i, value))
                # both cmp and key are given
                return sum(1 for i in self if cmp(key(i), value))

class Person:
        def __init__(self, first_name, last_name, age, gender):
                self.first_name, self.last_name, self.age, self.gender
= \
                first_name, last_name, age, gender

a = slist([3, 5, 7, 3])
print "a =", a
print "a has", a.count(3), "3's and", a.count(4), "4's."
print "a has", a.count(4, cmp=gt), "elements > 4 and", \
        a.count(5, cmp=le), "elements <= 5."

b = slist( [ Person("John", "Smith", 30, 'm'), Person("Claire", "Doe",
23, 'f'), \
        Person("John", "Black", 43, 'm'), Person("Anne", "Jolie", 50,
'f') ] )
print "b has", b.count("John", key=attrgetter("first_name")), \
        "elements with first_name == John."
print "b has", b.count(25, cmp=le, key=attrgetter("age")), \
        "elements with age <= 25."

$ ./slist.py
a = [3, 5, 7, 3]
a has 2 3's and 0 4's.
a has 2 elements > 4 and 3 elements <= 5.
b has 2 elements with first_name == John.
b has 2 elements with age <= 30.




More information about the Python-list mailing list