EXOR or symmetric difference for the Counter class
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Fri Aug 13 01:36:15 EDT 2010
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
More information about the Python-list
mailing list