EXOR or symmetric difference for the Counter class

Paddy paddy3118 at googlemail.com
Thu Aug 12 16:20:19 EDT 2010


I find myself needing to calculate the difference between two Counters
or multisets or bags.

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?

- Paddy.



More information about the Python-list mailing list