[ python-Bugs-1696199 ] Add collections.counts()

SourceForge.net noreply at sourceforge.net
Thu Apr 19 17:15:06 CEST 2007


Bugs item #1696199, was opened at 2007-04-07 15:38
Message generated for change (Comment added) made by bediviere
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1696199&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.6
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Steven Bethard (bediviere)
Assigned to: Raymond Hettinger (rhettinger)
Summary: Add collections.counts()

Initial Comment:
As suggested in http://mail.python.org/pipermail/python-list/2007-April/433986.html this is a patch to add a counts() function to the collections module. Usage looks like:

>>> items = 'acabbacba'
>>> item_counts = counts(items)
>>> for item in 'abcd':
...     print item, item_counts[item]
...
a 4
b 3
c 2
d 0

Yes, it's only a 4-line function, but it's a frequently re-written 4-line function.

----------------------------------------------------------------------

>Comment By: Steven Bethard (bediviere)
Date: 2007-04-19 09:15

Message:
Logged In: YES 
user_id=945502
Originator: YES

A summary of the python-dev thread
(http://mail.python.org/pipermail/python-dev/2007-April/072502.html)

Since the number of times an unseen item was seen is 0, most people felt
returning 0 was more natural behavior than raising KeyError.

There was some discussion of alternate names, but most people were fine
with counts().

Raymond suggested making it a classmethod of dict, but people were a
little concerned about adding to dict's already complex API, and since the
result of counts() needed to return 0s instead of raising KeyErrors, it
wouldn't really have the same behavior as a plain dict() anyway.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-04-07 18:23

Message:
Logged In: YES 
user_id=6380
Originator: NO

I guess it's simplicity of implementation vs. simplicity of use.  And I'm
not even sure which is easier to use.  It's just that defaultdicts are a
very new thing and still feel "weird" -- even though I pushed for the
implementation based on popular demand I'm not a user myself.  Perhaps ask
around on python-dev?



----------------------------------------------------------------------

Comment By: Steven Bethard (bediviere)
Date: 2007-04-07 17:48

Message:
Logged In: YES 
user_id=945502
Originator: YES

I think it's okay if it's not a defaultdict.  That was the simplest
implementation, but I certainly have no problem calling d.get() when
necessary. Should I change the implementation to use a dict()?

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-04-07 17:39

Message:
Logged In: YES 
user_id=6380
Originator: NO

Does it have to be a defaultdict?  I.e. is it important that
item_counts['d'] not raise KeyError?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1696199&group_id=5470


More information about the Python-bugs-list mailing list