I am trying to count the string and I want to delete the key with value 1 and 2. I am successfully able to count the strings with below script. If I am adding if loop to the dictionary its throwing Key error 'd' when I try to print it.
s = "aaabbbd"
def check_freq(s):
freq = {}
for c in s:
freq[c] = s.count(c)
for w in sorted(freq, key=freq.get, reverse=True):
if freq[w] == 1:
del freq[w]
print (w, freq[w])
check_freq(s)
Error is below
File "<ipython-input-60-a79c71ac1b31>", line 10, in check_freq
print (w, freq[w])
KeyError: 'd'
Expected output
{'a': 3, 'b': 3}