a_list.count(a_callable) ?

Ping ping.nsr.yeh at gmail.com
Sat Jun 16 04:12:07 EDT 2007


On 6 16 ,   2 06 , "BJörn Lindqvist" <bjou... at gmail.com> wrote:
>
> Maybe you could extend count() analogous to how sort() works:
>
> # L is a list of Person objects, each Person has a name attribute
> L.sort(key = attrgetter("name"))
>
> # How many olle are there?
> print L.count("olle", key = attrgetter("name"))
>
> # And index could be extended in the same way!
> # Whom has id 1234?
> print L.index(1234, key = attrgetter("id")).name
>
> All of these could be solved by giving Person an __eq__() method, but
> it fails when you need to search, sort or count on a different key.
>
> --
> mvh Björn

Wow! This jumps out of my screen!  I like it very much.
How to get the extension into the language?

cheers,
Ping

p.s. By the way, I guess you meant
print L[L.index(1234, key = attrgetter("id"))].name
in the index example.




More information about the Python-list mailing list