[ python-Feature Requests-739029 ] Constructor for counting things

SourceForge.net noreply at sourceforge.net
Sun Jul 17 16:07:21 CEST 2005


Feature Requests item #739029, was opened at 2003-05-17 04:22
Message generated for change (Comment added) made by birkenfeld
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=739029&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: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: David Albert Torpey (dtorp)
Assigned to: Nobody/Anonymous (nobody)
Summary: Constructor for counting things

Initial Comment:
Counting things is very common.  May I suggest an 
alternate dictionary constructor that works like this:

class BetterDictionary(dict):
    def bag(classobject, sequence):
        "Fast way to count things"
        b = classobject()
        for k in sequence:
            b[k] = b.get(k,0) + 1
        return b
    bag = classmethod(bag)

print BetterDictionary.bag("jack and jill went up a 
hill ...")

A C implementation could do this very fast.

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

>Comment By: Reinhold Birkenfeld (birkenfeld)
Date: 2005-07-17 16:07

Message:
Logged In: YES 
user_id=1188172

Perhaps "bag" is a good addition to the "collections" module.

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

Comment By: David Albert Torpey (dtorp)
Date: 2003-08-26 20:07

Message:
Logged In: YES 
user_id=681258

Smalltalk has a collection class called Bag that fills the same 
role.  It is a frequently used class.

The current way of doing things is too slow.  Disassembly 
shows that the whole process involves loading a constant for 
1, loading a constant for zero, loading the local variable 'b', 
performing an attribute lookup for 'get', loading the local 
variable 'k', calling 'get', running the summation, re-loading 
both 'b' and 'k' and doing a subscript store.  Along the way, 
there are two dictionary lookups for the same key.   All of 
that is just the inner loop and is wrapped in for-loop with all 
of its overhead.  Instead, the whole thing could be done in 
one step at C speed:  b.bag(itemlist).

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

Comment By: Brett Cannon (bcannon)
Date: 2003-08-12 08:12

Message:
Logged In: YES 
user_id=357491

I don't know if it is *that* common.  Besides, as your code shows, 
it isn't that complex.  It strikes me as a little too specific to be 
made a built-in dict constructor.

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

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


More information about the Python-bugs-list mailing list