Question

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

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)))
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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])

Add a comment
Know the answer?
Add Answer to:
I need to go through a text file with strings separated by a space. I would...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT