Comparing values of counter in python 3.3

Wolfgang Maier xpysol at gmail.com
Thu Dec 12 10:55:26 CET 2013


> I want to print only key,values in Counter2 which have values > then corresponding value in Counter1.
> E.g
> Counter1={97:1,99:2,196:2,198:1}
> Counter2={97:1 ,99:3, 196:1,198:1}
> 
> # Output
> [99,3]
>

Try:

[[key, Counter2[key]] for key in Counter1 if Counter2[key] > Counter1[key]]

for a start.
If you can't guarantee that every key from Counter1 is also in Counter2 you could use something like:

[[key, Counter2[key]] for key in Counter1 if key in Counter2 and Counter2[key] > Counter1[key]]

Best,
Wolfgang



More information about the Python-list mailing list