Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #84129
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '"""': 0.07; '21,': 0.07; 'element': 0.07; 'elements.': 0.07; 'initialize': 0.07; '"""add': 0.09; '"""create': 0.09; 'counting': 0.09; 'expected.': 0.09; 'falls': 0.09; 'keys,': 0.09; 'occurrences': 0.09; 'repeated': 0.09; 'subset': 0.09; 'todo:': 0.09; 'api': 0.11; 'def': 0.12; 'jan': 0.12; '"""remove': 0.16; '"""return': 0.16; "%s'": 0.16; '@classmethod': 0.16; '__all__': 0.16; '__lt__(self,': 0.16; '__ne__(self,': 0.16; '__or__(self,': 0.16; 'add(self,': 0.16; 'argument,': 0.16; 'bags.': 0.16; 'boolean': 0.16; 'caching': 0.16; 'clear(self):': 0.16; 'collections': 0.16; 'counter):': 0.16; 'hashable': 0.16; 'in-place': 0.16; 'intersection': 0.16; 'item):': 0.16; 'iterable': 0.16; 'length.': 0.16; 'list(self)': 0.16; 'mapping,': 0.16; 'merely': 0.16; 'multiset.': 0.16; 'set(self)': 0.16; 'symmetric': 0.16; 'underlying': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'unlike': 0.19; 'value.': 0.19; 'import': 0.22; 'skip:+ 20': 0.22; 'creating': 0.23; 'new,': 0.24; '(or': 0.24; 'second': 0.26; 'skip:_ 20': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; 'comparison': 0.31; 'consequence': 0.31; "d'aprano": 0.31; 'keys': 0.31; 'steven': 0.31; 'class': 0.32; 'another': 0.32; '(i.e.': 0.33; 'cases': 0.33; 'skip:_ 10': 0.34; 'skip:s 30': 0.35; 'equal': 0.35; 'operations': 0.35; 'usual': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'curious': 0.36; 'positive': 0.37; 'mapping': 0.38; 'to:addr :python-list': 0.38; 'short': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'remove': 0.60; 'negative': 0.60; 'length': 0.61; 'back': 0.62; 'times': 0.62; 'name': 0.63; 'such': 0.63; 'skip:n 10': 0.64; 'provide': 0.64; 'more': 0.64; 'different': 0.65; 'containing': 0.69; 'union': 0.69; 'evaluate': 0.72; 'bag': 0.74; '2015': 0.84; 'bag,': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=YGsBL4GA4WuAKAmdkhkope7Le9crxsGUJsowJWzwjwE=; b=tihr3Pf/uTHcPvv+w+ApI26WLc21PUC1ecSOlyntGWe23nTsC2AZ2T5xVWei4lnTnU i2MwYmuUbtjzNvH7xWyIzpimlkWJYBQQYBqSkNZ8DRJLNs8WAiSMW9XbDGyV6WmJkQX9 Tb6GSBXnmVRITIi3SdOyGyH6GZ0spRgweJDQdO+bj4XzOBlJH+oDNcjH+6lFcNiSGr9h CVBV038ds+RLeoMHero29+CKrgCYmStI+p+SNkh8F/YWqTdCy++jaC7Z0Dyr7mQyeyTU QFlapxUgWRYTVWEQNR9gkhjs9vjDg95PLQkJOCplz0Tip7NQhayl6tpZkoIdS9GK4g4W sXYw== |
| X-Received | by 10.66.120.109 with SMTP id lb13mr64613653pab.66.1421856965713; Wed, 21 Jan 2015 08:16:05 -0800 (PST) |
| MIME-Version | 1.0 |
| In-Reply-To | <54bfbc1d$0$13007$c3e8da3$5496439d@news.astraweb.com> |
| References | <CAG=hEY1L-39EmuWpdEh_n-BNfs=qG9nL=MrMT0ar72yGBrkoUA@mail.gmail.com> <mailman.17884.1421734095.18130.python-list@python.org> <87ppa9944t.fsf@elektro.pacujo.net> <mailman.17906.1421827045.18130.python-list@python.org> <b37db482-1c2d-4b17-9b0b-0e37537482ef@googlegroups.com> <54bfbc1d$0$13007$c3e8da3$5496439d@news.astraweb.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Wed, 21 Jan 2015 09:15:25 -0700 |
| Subject | Re: Trees |
| To | Python <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17917.1421857401.18130.python-list@python.org> (permalink) |
| Lines | 217 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1421857401 news.xs4all.nl 2863 [2001:888:2000:d::a6]:53601 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:84129 |
Show key headers only | View raw
On Wed, Jan 21, 2015 at 7:47 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>> More irksome that for the second we've to preface with
>>
>> from collections import Counter
>>
>> And still more a PITA that a straightforward standard name like bag (or
>> multiset) is called by such an ungoogleable misleading name as counter.
>
> It is not misleading. collections.Counter is a class for counting (i.e. a
> counter), not a bag. It is merely *similar* to a bag, but the API is not
> the same as the usual bag API because the motivating design is not the same
> as for bags.
To expand on this, Counter does provide a few multiset operations like
union and intersection. But there are a number of cases where it falls
short or does not perform as expected. To name a few:
* There are no subset or superset comparison operations provided.
* len(counter) returns the number of keys, not the number of elements.
* Unlike a multiset, Counters can have negative or zero counts; one
curious consequence of this is that an "empty" Counter can have a
non-zero length and evaluate as true in boolean contexts.
A while back I fiddled around with creating a true MultiSet class
using a Counter as the underlying implementation. Here's what I came
up with:
from collections import Counter
from collections.abc import MutableSet, Set
__all__ = ['MultiSet']
class MultiSet(MutableSet):
"""Multiset class containing hashable items."""
# Class invariants:
# * self._counter is a Counter s/t all values are positive ints denoting
# multiplicity.
# * set(self) == self._counter.keys()
def __init__(self, iterable_or_mapping=()):
"""Create a new, empty MultiSet. If passed an iterable argument, initialize
the MultiSet with items from the iterable. If passed a mapping argument,
initialize the MultiSet with the keys of the mapping, each repeated a number
of times equal to the associated value.
"""
self._counter = +Counter(iterable_or_mapping)
@classmethod
def _from_counter(cls, counter):
self = cls.__new__(cls)
self._counter = counter
return self
def __contains__(self, item):
return item in self._counter
def __hash__(self):
raise TypeError('unhashable type: %s' % type(self))
def __iter__(self):
return self._counter.elements()
def __len__(self):
# TODO: consider caching the length.
return sum(self._counter.values())
def __repr__(self):
if self:
return 'MultiSet(%r)' % list(self)
return 'MultiSet()'
def add(self, item):
"""Add an element to the MultiSet."""
self._counter[item] += 1
def clear(self):
"""Remove all elements from the MultiSet, leaving it empty."""
self._counter.clear()
def count(self, item):
"""Return the number of occurrences of the element."""
return self._counter[item]
def discard(self, item):
"""Remove an element from the MultiSet.
If there are multiple occurrences, remove only one such occurrence. If the
element is not a member, do nothing.
"""
if item not in self._counter:
return
if self._counter[item] == 1:
del self._counter[item]
else:
self._counter[item] -= 1
def __or__(self, other):
"""Return the union of the MultiSet and another set."""
if not isinstance(other, Set):
return NotImplemented
return self._from_counter(self._counter | _get_counter(other))
__ror__ = __or__
def __ior__(self, other):
"""Perform the in-place union of the MultiSet and another set."""
if not isinstance(other, Set):
return NotImplemented
self._counter |= _get_counter(other)
return self
def __and__(self, other):
"""Return the intersection of the MultiSet and another set."""
if not isinstance(other, Set):
return NotImplemented
return self._from_counter(self._counter & _get_counter(other))
__rand__ = __and__
def __iand__(self, other):
"""Perform the in-place intersection of the MultiSet and another set."""
if not isinstance(other, Set):
return NotImplemented
self._counter &= _get_counter(other)
return self
def __sub__(self, other):
"""Return the difference of the MultiSet and another set."""
if not isinstance(other, Set):
return NotImplemented
return self._from_counter(self._counter - _get_counter(other))
def __rsub__(self, other):
"""Return the difference of another set and the MultiSet."""
if not isinstance(other, Set):
return NotImplemented
return self._from_counter(_get_counter(other) - self._counter)
def __isub__(self, other):
"""Perform the in-place set difference of the MultiSet and another set."""
if not isinstance(other, Set):
return NotImplemented
self._counter -= _get_counter(other)
return self
def __xor__(self, other):
"""Return the symmetric difference of the MultiSet and another set."""
if not isinstance(other, Set):
return NotImplemented
# X ^ Y == (X - Y) | (Y - X)
other_counter = _get_counter(other)
counter = (self._counter - other_counter) | (other_counter - self._counter)
return self._from_counter(counter)
__rxor__ = __xor__
def __ixor__(self, other):
"Perform the in-place symmetric difference of the MultiSet and another set."
if not isinstance(other, Set):
return NotImplemented
# X ^ Y == (X - Y) | (Y - X)
other_counter = _get_counter(other)
other_diff = other_counter - self._counter
self._counter -= other_counter
self._counter |= other_diff
return self
def __eq__(self, other):
"""Return whether the MultiSet and the other set have the same contents."""
if not isinstance(other, Set):
return NotImplemented
return self._counter == _get_counter(other)
def __ne__(self, other):
"""Return whether the MultiSet and the other set have different contents."""
if not isinstance(other, Set):
return NotImplemented
return self._counter != _get_counter(other)
def __le__(self, other):
"""Return whether the MultiSet is a subset of the other set."""
if not isinstance(other, Set):
return NotImplemented
other_counter = _get_counter(other)
return all(self._counter[x] <= other_counter[x] for x in self._counter)
def __lt__(self, other):
"""Return whether the MultiSet is a proper subset of the other set."""
if not isinstance(other, Set):
return NotImplemented
return self.__le__(other) and not self.__eq__(other)
def __ge__(self, other):
"""Return whether the MultiSet is a superset of the other set."""
if not isinstance(other, Set):
return NotImplemented
other_counter = _get_counter(other)
return all(self._counter[x] >= other_counter[x] for x in other_counter)
def __gt__(self, other):
"""Return whether the MultiSet is a proper superset of the other set."""
if not isinstance(other, Set):
return NotImplemented
return self.__ge__(other) and not self.__eq__(other)
def _get_counter(maybe_multiset):
if isinstance(maybe_multiset, MultiSet):
return maybe_multiset._counter
else:
return Counter(maybe_multiset)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Trees Terry Reedy <tjreedy@udel.edu> - 2015-01-20 01:08 -0500
Re: Trees Marko Rauhamaa <marko@pacujo.net> - 2015-01-20 11:45 +0200
Re: Trees Paul Rubin <no.email@nospam.invalid> - 2015-01-20 10:14 -0800
Re: Trees Marko Rauhamaa <marko@pacujo.net> - 2015-01-20 22:26 +0200
Re: Trees Stephen Hansen <me+python@ixokai.io> - 2015-01-20 23:56 -0800
Re: Trees Marko Rauhamaa <marko@pacujo.net> - 2015-01-21 10:35 +0200
Re: Trees Rustom Mody <rustompmody@gmail.com> - 2015-01-21 04:09 -0800
Re: Trees Chris Angelico <rosuav@gmail.com> - 2015-01-21 23:35 +1100
Re: Trees Rustom Mody <rustompmody@gmail.com> - 2015-01-21 07:24 -0800
Re: Trees Tim Chase <python.list@tim.thechases.com> - 2015-01-21 06:55 -0600
Re: Trees Chris Angelico <rosuav@gmail.com> - 2015-01-22 00:01 +1100
Re: Trees Tim Chase <python.list@tim.thechases.com> - 2015-01-21 08:26 -0600
Re: Trees Chris Angelico <rosuav@gmail.com> - 2015-01-22 01:31 +1100
Re: Trees Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-22 01:47 +1100
Re: Trees Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-21 09:15 -0700
Re: Trees Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-21 10:27 -0700
Re: Trees Rustom Mody <rustompmody@gmail.com> - 2015-01-20 05:33 -0800
Re: Trees Rustom Mody <rustompmody@gmail.com> - 2015-01-20 05:51 -0800
Re: Trees Marko Rauhamaa <marko@pacujo.net> - 2015-01-20 16:15 +0200
Re: Trees Rustom Mody <rustompmody@gmail.com> - 2015-01-20 06:35 -0800
Re: Trees Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-20 10:19 -0700
Re: Trees Rustom Mody <rustompmody@gmail.com> - 2015-01-20 10:15 -0800
Re: Trees Rustom Mody <rustompmody@gmail.com> - 2015-01-20 10:35 -0800
Re: Trees Mario <marfig@gmail.com> - 2015-01-20 22:47 +0100
Re: Trees Rustom Mody <rustompmody@gmail.com> - 2015-01-20 17:23 -0800
Re: Trees Paul Rubin <no.email@nospam.invalid> - 2015-01-20 17:49 -0800
Re: Trees Rustom Mody <rustompmody@gmail.com> - 2015-01-20 18:03 -0800
Re: Trees Paul Rubin <no.email@nospam.invalid> - 2015-01-21 14:27 -0800
Re: Trees Rustom Mody <rustompmody@gmail.com> - 2015-01-21 21:17 -0800
Re: Trees Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-21 15:54 -0700
Re: Trees Rustom Mody <rustompmody@gmail.com> - 2015-01-21 21:20 -0800
Re: Trees Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-22 00:01 -0700
Re: Trees Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-21 23:56 -0700
Re: Trees Paul Rubin <no.email@nospam.invalid> - 2015-01-21 23:16 -0800
Re: Trees Rustom Mody <rustompmody@gmail.com> - 2015-01-22 08:54 -0800
Re: Trees Terry Reedy <tjreedy@udel.edu> - 2015-01-20 21:19 -0500
Re: Trees Mario Figueiredo <marfig@gmail.com> - 2015-01-21 14:05 +0000
csiph-web