Friday, 17 December 2021

Python-Slip 12-B-Write a python program to count repeated characters in a string. Sample string: 'thequickbrownfoxjumpsoverthelazydog'

 Write a python program to count repeated characters in a string. Sample string: 'thequickbrownfoxjumpsoverthelazydog'

 import collections

str1 = 'thequickbrownfoxjumpsoverthelazydog'

d = collections.defaultdict(int)

for c in str1:

d[c] += 1

for c in sorted(d, key=d.get, reverse=True):

if d[c] > 1:

print('%s - %d' % (c, d[c]))