Apr-02-2019, 11:17 AM
def count(string): # Create empty dictionary out out = {} # for each letter in the string for letter in string: #if the letter's not already in the dictionary if letter not in out: # add letter to dictionary and set value to 0 out[letter] = 0 # now increment the letter's value out[letter] = out[letter] + 1 return outstring = 'cache'
As an example structure of the dictionary for above string would look like:
out = { 'a': 1, 'c': 2, 'e': 1, 'h': 1 }