Skip to content

Instantly share code, notes, and snippets.

@defuz
Created October 1, 2012 14:19
Show Gist options
  • Save defuz/3812061 to your computer and use it in GitHub Desktop.
Save defuz/3812061 to your computer and use it in GitHub Desktop.
occurences of each character in a string
# for http://stackoverflow.com/questions/12674193/python-no-of-occurences-of-each-character-in-a-string
def xx(string):
i, d = 0, {}
while i < 127:
char = chr(i)
d[char] = string.count(char)
i += 1
return d
# printable characters only:
from string import printable
def xxxx(string):
return {c: string.count(c) for c in printable}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment