Re: a_list.count(a_ callable) ?
On 6 16 , 2 06 , "BJörn Lindqvist" <bjou...@gmail. comwrote:
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.
On 6 16 , 2 06 , "BJörn Lindqvist" <bjou...@gmail. comwrote:
>
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("nam e"))
>
# How many olle are there?
print L.count("olle", key = attrgetter("nam e"))
>
# 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
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("nam e"))
>
# How many olle are there?
print L.count("olle", key = attrgetter("nam e"))
>
# 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
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.
Comment