enhancing 'list'

Terry Reedy tjreedy at udel.edu
Mon Jan 18 02:56:16 EST 2010


On 1/17/2010 5:37 PM, samwyse wrote:
> Consider this a wish list.  I know I'm unlikely to get any of these in
> time for for my birthday, but still I felt the need to toss it out and
> see what happens.
>
> Lately, I've slinging around a lot of lists, and there are some simple
> things I'd like to do that just aren't there.
>
> s.count(x[, cmp[, key]])
> - return number of i‘s for which s[i] == x.  'cmp' specifies a custom
> comparison function of two arguments, as in '.sort'.  'key' specifies
> a custom key extraction function of one argument.
> s.index(x[, i[, j[, cmp[, key]]]])
> - return smallest k such that s[k] == x and i<= k<  j.  'cmp' and
> 'key' are as above.
> s.rindex(x[, i[, j[, cmp[, key]]]])
> - return largest k such that s[k] == x and i<= k<  j.  'cmp' and
> 'key' are as above.
>
> There are two overlapping proposals here.  One is to add the .rindex
> method, which strings already have.  The other is to extend the
> optional arguments of .sort to all other methods that test for item
> equality.
>
> One last thing, the Python 2.6.2 spec says .count and .index only
> apply to mutable sequence types.  I see no reason why they
> (and .rindex) couldn't also apply to immutable sequences (tuples, in
> particular).

In 3.x, tuple does have those methods, even though the doc is not clear 
(unless fixed by now).





More information about the Python-list mailing list