Comparing values of counter in python 3.3

Devin Jeanpierre jeanpierreda at gmail.com
Mon Dec 16 04:50:41 EST 2013


On Sun, Dec 15, 2013 at 6:32 PM, rusi <rustompmody at gmail.com> wrote:
> On Monday, December 16, 2013 7:29:31 AM UTC+5:30, alex23 wrote:
>> > # Need to compare values of counter and reject in function/routine in value in counter2 is higher then value in counter1 for a current key
>
>>      [(k,Counter2[k]) for k in Counter2 - Counter1]
>
> Why not just?
>
> Counter2 - Counter1
>
> And if you want to uncounterify it then
> dict(Counter2 - Counter1)

Because you get different counts.

>>> c1 = Counter('ab')
>>> c2 = Counter('aab')
>>> c2 - c1
Counter({'a': 1})
>>> [(k, c2[k]) for k in c2 - c1]
[('a', 2)]

Counter subtraction is multiset subtraction, not set subtraction.

-- Devin



More information about the Python-list mailing list