From: samwyse on
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).
From: Asun Friere on
On Jan 18, 9:37 am, samwyse <samw...(a)gmail.com> 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.
>

If memory serves me correctly, it has been possible to subclass 'built-
in' types since Py2.2 or thereabouts.
From: Terry Reedy on
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).


From: Peter Otten on
samwyse wrote:

> 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.

What's your use case exactly? If I were to enhance count/index/rindex I
would go for the simpler

>>> missing = object()
>>> class List(list):
.... def count(self, value=missing, predicate=missing):
.... if value is missing:
.... if predicate is missing:
.... raise TypeError
.... return sum(1 for item in self if predicate(item))
.... else:
.... if predicate is not missing:
.... raise TypeError
.... return list.count(self, value)
....
>>> items = List(range(10))
>>> items.count(7)
1
>>> items.count(predicate=lambda item: item%3)
6

which nicely covers all applications I can imagine.

Peter
From: samwyse on
On Jan 18, 1:56 am, Terry Reedy <tjre...(a)udel.edu> wrote:
> 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).

That's good to hear. Perhaps I should have tried them directyly, but
my 3.1 docs still echo the 2.x docs, which only show them for
immutable sequences.
 |  Next  |  Last
Pages: 1 2 3
Prev: A simple-to-use sound file writer
Next: using super