Feature Requests item #739029, was opened at 2003-05-17 02:22
Message generated for change (Settings changed) made by gbrandl
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: Closed
>Resolution: Rejected
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: Georg Brandl (gbrandl)
Date: 2006-03-17 19:33

Message:
Logged In: YES 
user_id=849994

I think this a reasonable use case for defaultdict:

d = defaultdict(int)
for k in seq:
    d[k] += 1

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

Comment By: Michael Chermside (mcherm)
Date: 2005-12-28 17:19

Message:
Logged In: YES 
user_id=99874

I think I prefer birkenfeld's approach: create a well-tuned
implementation for the collections module rather than adding
yet one MORE use case to the already overburdened (yet very
powerful) dict.

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

Comment By: Georg Brandl (birkenfeld)
Date: 2005-07-17 14: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 18: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 06: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
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to