I need to go through a text file with strings separated by a space. I would like to have this code be able to read each word and count the number of recurring letters in each word. For example apple would have an output of :
apple
a 1
p 2
l 1
e 1
however, when i run this code I am getting the output of 1's for each word.
s = 'apple'
for line in s:
keys = [] # list to save characters
line = line.strip() # trim spaces from beginning and end
words = line.split() # split line by space
for word in words: # for every word in words
keys = keys + list(word) # get the characters
character_dict = set(keys) # A set to store non redundant
for key in character_dict:
print("%s\t%d" % (key, keys.count(key)))
line = 'apple'
keys = []
for x in line:
keys.append(x)
d = dict()
for y in keys:
if (not y in d.keys()):
d[y] = 1
else:
d[y] += 1
for z in d.keys():
print(z,d[z])

I need to go through a text file with strings separated by a space. I would...