I have two collections.defaultdict and trying to remove values from d1 that are also in d2.
from collections import Counter, defaultdict
d1 = Counter({'hi': 22, 'bye': 55, 'ok': 33})
d2 = Counter({'hi': 10, 'hello': 233, 'nvm': 96})
Ideal result:
d3 = set()
d3 = ({'bye':55, 'ok':33})
So far I have tried:
d3 = set()
d3 = d1 - d2
print(d3)
Counter({'bye': 55, 'ok': 33, 'hi': 12})
But this keeps the same value of 'hi' even though I want to remove all similar ones.
Since, d1 and d2 are Counter objects they implement subtraction different than sets.
From
collections.Counter(emphasis mine):Addition and subtraction combine counters by adding or subtracting the counts of corresponding elements.
From
set.differenceorset - other:Return a new set with elements in the set that are not in the others.
That said, you can use Counter.keys and use difference just like sets.
keys = d1.keys() - d2.keys()
# keys = {'bye', 'ok'}
out = {k: d1[k] for k in keys}
# out = {'bye': 55, 'ok': 33}
Use a dictionary comprehension
d3 = {k: v for k, v in d1.items() if k not in d2}
print(d3)
Result:
{'bye': 55, 'ok': 33}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With