On 14 Aug, 18:14, Raymond Hettinger <pyt...@rcn.com> wrote: > On Aug 12, 1:20 pm, Paddy <paddy3...@googlemail.com> wrote: > > > 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. > > Tell us about your use cases. I'm curious how a program would ascribe > semantic meaning to the result. The phrase "unique to each bag" > doesn't quite cover it, perhaps something like "number in either > source above the minimum held in common". > > AFAICT, I've never needed something like this as a primitive. Even > the xor operation for regular sets is rarely used. > > > I know how to > > calculate it: > > > >>> b = Counter(a=1, b=2) > > >>> c = Counter(a=3, b=1) > > >>> diff = (b - c) + (c - b) > > >>> diff > > Counter({'a': 2, 'b': 1}) > > That seems simple enough. > You could also use: > > diff = (b | c) - (b & c) # max(b,c) - min(b,c) > > Raymond
Hi Raymond and others, Lets say you have two *sets* of integers representing two near-copies of some system, then a measure of their difference could be calculated as: 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 originally. Thanks. -- http://mail.python.org/mailman/listinfo/python-list