From: samwyse on
On Jan 18, 3:06 am, Peter Otten <__pete...(a)web.de> wrote:
> 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

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.

>>> class List(list):
import __builtin__
def count(self, value, cmp=__builtin__.cmp):
return sum(1 for item in self if not cmp(item, value))


>>> items = List(range(10))
>>> items.count(7)
1
>>> items.count(3, lambda a, b: not a%b) # My way
6
>>> items.count(Ellipsis, lambda a, b: not a%3) # Your way
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".
From: samwyse on
On Jan 17, 11:30 pm, Asun Friere <afri...(a)yahoo.co.uk> wrote:
> 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.

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).
From: Peter Otten on
samwyse wrote:

> On Jan 18, 3:06 am, Peter Otten <__pete...(a)web.de> wrote:
>> 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
>
> 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.

>>>> class List(list):
> import __builtin__
> def count(self, value, cmp=__builtin__.cmp):
> return sum(1 for item in self if not cmp(item, value))
>
>
>>>> items = List(range(10))
>>>> items.count(7)
> 1
>>>> items.count(3, lambda a, b: not a%b) # My way
> 6
>>>> items.count(Ellipsis, lambda a, b: not a%3) # Your way
> 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
From: samwyse on
On Jan 18, 6:20 am, Peter Otten <__pete...(a)web.de> wrote:
> 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.)

> samwyse wrote:
> > 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...

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!
From: Stefan Behnel on
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
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: A simple-to-use sound file writer
Next: using super