[Python-ideas] Proposal for extending the collections module - bags / multisets, ordered sets / unique lists

Chris Rebert pyideas at rebertia.com
Sat Jul 18 08:30:44 CEST 2009


On Fri, Jul 17, 2009 at 11:18 PM, Chris Rebert<pyideas at rebertia.com> wrote:
>> On 07/17/2009 09:58 PM, Nick Coghlan wrote:
>>
>> Michael Lenzen wrote:
>>
>>
>> In a nutshell, I want to add 2 new classes (and respective frozen
>> varients) to the collections module, namely a bag (multiset) class and a
>> setlist (ordered set/unique list) class.  I know this has been floated
>> before, but I think it merits more consideration.
>>
>>
>> A Counter class (aka bag/multiset) and an OrderedDict class were added
>> to collections for 3.1. These classes will also be available in 2.7.
>>
>> Counter *is* a bag implementation, while OrderedDict can trivially be
>> used as the basis for an OrderedSet implementation.
> On Fri, Jul 17, 2009 at 9:57 PM, Michael Lenzen<m.lenzen at gmail.com> wrote:
>> I posted this in the wrong thread before, sorry.
>>
>> A Counter class is NOT a multiset/bag, it allows negative and zero
>> elements.  This makes as much sense to me as having negative elements in a
>> Set.  This is all besides the fact that Counter is nothing more than
>> defaultdict(int) so it should be removed regardless of whether or not bags
>> are added.
>
> Truth be told, it's more than just defaultdict(int). It adds
> .elements() and .most_common().
>
> Seems bag-like to me.
> - Unordered? Check.
> - Allows duplicates? Check.
> - O(1) containment test? Check.
> - Counts multiplicity of elements? Check.
> - Iterable? Check.
>
> The only non-bag thing about it is allowing 0 and negative
> multiplicities, which I agree is unintuitive; I don't like that
> "feature" either.

Actually, from the docs, it also appears (I don't have 3.0 handy to
test) to get len() wrong, using the dict definition of "number of
unique items" rather than just "number of items" as would be more
appropriate for a bag.

In the event a Bag is not added, +1 for adding a method to Counter to
return `sum(count if count > 0 else 0 for count in
a_counter.values())`

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-ideas mailing list