EXOR or symmetric difference for the Counter class

Paddy paddy3118 at googlemail.com
Fri Aug 13 18:19:55 EDT 2010


On Aug 13, 6:36 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Thu, 12 Aug 2010 13:20:19 -0700, Paddy wrote:
> > I find myself needing to calculate the difference between two Counters
> > or multisets or bags.
>
> Is this collections.Counter from Python 3.1? If so, you should say so,
> and if not, you should tell us which Counter class this is. It will save
> people (by which I mean *me*) from spending an unrewarding few minutes
> trying to import Counter in Python 2.5 and writing up a sarcastic
> response ... :)
>
> > I want those items that are unique to each bag. I know how to calculate
> > it:
>
> >     >>> b = Counter(a=1, b=2)
> >     >>> c = Counter(a=3, b=1)
> >     >>> diff = (b - c) + (c - b)
> >     >>> (b - c)
> >     Counter({'b': 1})
> >     >>> (c - b)
> >     Counter({'a': 2})
> >     >>> diff
> >     Counter({'a': 2, 'b': 1})
>
> > But thought why doesn't this operation appear already as a method of the
> > class?
>
> Don't know. Perhaps you should put in a feature request.
>
> --
> Steven

Yes it is Counter in both 3.1 and 2.7 (And somewhere on Activestate
too).

Before I put in a feature request, I wanted to know if other have seen
the need.



More information about the Python-list mailing list