enhancing 'list'

S

samwyse

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 == 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).
 
A

Asun Friere

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

Terry Reedy

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 == 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).
 
P

Peter Otten

samwyse said:
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 == 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
.... 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)
....6

which nicely covers all applications I can imagine.

Peter
 
S

samwyse

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

samwyse

samwyse said:
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 == 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

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

which nicely covers all applications I can imagine.

Peter


That is a good idea. However, I was looking more at the simplicity of
building of ideas that are already present in .sort. And this
implementation is pretty simple as well.
import __builtin__
def count(self, value, cmp=__builtin__.cmp):
return sum(1 for item in self if not cmp(item, value))

6

As a side note, wouldn't it be nice if '...' could be used in more
places than just slices? IMHO, a useful idiom would be to use it to
signify "irrelevant" or "don't care", as opposed to 'None' which (in
my mind, at least) signifies "missing" or "unknown".
 
S

samwyse

If memory serves me correctly, it has been possible to subclass 'built-
in' types since Py2.2 or thereabouts.

True, but I've had bad experiences doing that. See, for example,
http://groups.google.com/group/comp.lang.python/browse_thread/thread/10cfe2affc265ac
where I tried to subclass 'int'. More importantly, subclassing means
that people have to keep re-inventing the same methods. Having a
single implementation would save time, not to mention the speed
advantages of implementing them in the hosting language (C,
Java, .NET, etc).
 
P

Peter Otten

samwyse said:
samwyse said:
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 == 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


That is a good idea. However, I was looking more at the simplicity of
building of ideas that are already present in .sort. And this
implementation is pretty simple as well.


Note that the cmp() builtin and the cmp parameter for list.sort() are gone
in Python 3.
import __builtin__
def count(self, value, cmp=__builtin__.cmp):
return sum(1 for item in self if not cmp(item, value))


6

As a side note, wouldn't it be nice if '...' could be used in more
places than just slices? IMHO, a useful idiom would be to use it to
signify "irrelevant" or "don't care", as opposed to 'None' which (in
my mind, at least) signifies "missing" or "unknown".

That is a pretty subtle distinction...

I prefer keyword arguments, but in Python 3 you can use the ellipsis literal
freely:
... == ... True
[..., 42, ...].count(...)
2

Peter
 
S

samwyse

Note that the cmp() builtin and the cmp parameter for list.sort() are gone
in Python 3.

I've got Python 3 installed, and am using it for most new
development. In this case case, however, I'm writing for the Google
App Engine, which is stuck at 2.5. :( (Curiously, no matter how I
order my PATH, the wrong version seems to appear first more than half
the time! I'm seriously considering renaming all my Python 3 code to
use a .py3 file extension.)
That is a pretty subtle distinction...

Hey, I'm a pretty subtle person...
I prefer keyword arguments, but in Python 3 you can use the ellipsis literal
freely:
... == ... True
[..., 42, ...].count(...)

2

Peter

I must have either missed or forgotten about that. Thanks!
 
S

Stefan Behnel

samwyse, 18.01.2010 13:49:
Curiously, no matter how I
order my PATH, the wrong version seems to appear first more than half
the time! I'm seriously considering renaming all my Python 3 code to
use a .py3 file extension.

You should be able to start the interpreter as "python3.1" to be sure.

Stefan
 
S

Steve Holden

samwyse said:
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 == 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.


The tuple IS an immutable sequence.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top