Prev: Decompressing a file retrieved by URL seems too complex
Next: Decompressing a file retrieved by URL seems too complex
From: Paddy on 12 Aug 2010 16:20 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.
From: Steven D'Aprano on 13 Aug 2010 01:36 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
From: Chris Rebert on 13 Aug 2010 01:45 On Thu, Aug 12, 2010 at 10:36 PM, Steven D'Aprano <steve(a)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? "Changed in version **2.7**: Added Counter and OrderedDict." That is still ahead of the curve though. Cheers, Chris
From: Paddy on 13 Aug 2010 18:19
On Aug 13, 6:36 am, Steven D'Aprano <st...(a)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. |