EXOR or symmetric difference for the Counter class
python at rcn.com
Tue Aug 17 03:29:33 CEST 2010
> Lets say you have two *sets* of integers representing two near-copies
> of some system, then a measure of their difference could be calculated
> len(X.symmetric_difference(Y)) / (len(X) + len(Y)) * 100 %
> If the two collections of integers are allowed duplicates then you
> need a Counter/bag/multi-set type and the diff calculation I gave
Thanks for sharing your use case.
It's unlikely that I will add this method to the Counter API because
the rarity of use case does not warrant the added API complexity.
IMO, adding a method like this makes the class harder to learn,
understand and remember. It doesn't seem like much of a win over
using the existing alternatives:
* (b - c) + (c - b)
* (b | c) - (b & c)
* DIY using the two counters as simple dicts
* writing a subclass providing additional binary operations
I would like to see someone post a subclass to the ASPN Cookbook that
adds a number of interesting, though not common operations. Your
symmetric_difference() method could be one. A dot_product() operation
could be another. Elementwise arithmetic is another option (we
already have add and subtract, but could possibly use multiply,
divide, etc). Scaling operations are another possibility (multiple
all elements by five, for example).
The Counter() class has low aspirations. It is a dictionary that
fills-in missing values with zero and is augmented by a handful of
basic methods for managing the counts.
More information about the Python-list