Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Count occurrence of character in a string.
- # Ex: 'Hello World!' would have 2 'o'
- import itertools
- S = 'Hello World!'
- # Sort the string so each character comes consecutively
- srt = ''.join(sorted(S))
- # Group each characters
- grp = [ ''.join(grp) for num, grp in itertools.groupby(srt)]
- # Count each group
- hasil = map(lambda x: (x[0], len(x)), grp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement