[Python-ideas] Proposal for extending the collections module - bags / multisets, ordered sets / unique lists
pyideas at rebertia.com
Sat Jul 18 08:18:15 CEST 2009
> 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
+0.75 on adding a proper Bag. It could be subclassed from or borrow
code from Counter.
More information about the Python-ideas