I am lazy, so I am going to use library functions to get the job done for me:
import itertools
import collections
d = {"a": ["MRS", "VAL"], "b": "PRS", "c": "MRS", "d": "NTS"}
values = [[x] if isinstance(x, basestring) else x for x in d.values()]
counter = collections.Counter(itertools.chain.from_iterable(values))
print counter
print counter['MRS'] # Sampling
Output:
Counter({'MRS': 2, 'NTS': 1, 'PRS': 1, 'VAL': 1})
2
At the end, counter acts like the dictionary you want.
Explanation
Consider this line:
values = [[x] if isinstance(x, basestring) else x for x in d.values()]
Here, I turned every value in the dictionary d
into a list to make processing easier. values
might look something like the following (order might be different, which is fine):
# values = [['MRS', 'VAL'], ['MRS'], ['PRS'], ['NTS']]
Next, the expression:
itertools.chain.from_iterable(values)
returns a generator which flatten the list, conceptually, the list now looks like this:
['MRS', 'VAL', 'MRS', 'PRS', 'NTS']
Finally, the Counter class takes that list and count, so we ended up with the final result.