[Tutor] Alice_in_wonderland Question

Jake Blank jakenblank at gmail.com
Sun May 4 23:04:01 CEST 2014


Hi,
So I'm doing a problem on the Alice_in_wonderland.txt where I have to write
a program that reads a piece of text from a file specified by the user,
counts the number of occurrences of each word, and writes a sorted list of
words and their counts to an output file. The list of words should be
sorted based on the counts, so that the most popular words appear at the
top. Words with the same counts should be sorted alphabetically.

My code right now is

word_count = {}
file = open ('alice_in_wonderland.txt', 'r')
full_text = file.read().replace('--',' ')
full_text_words = full_text.split()

for words in full_text_words:
        stripped_words = words.strip(".,!?'`\"- ();:")
        try:
            word_count[stripped_words] += 1
        except KeyError:
            word_count[stripped_words] = 1

ordered_keys = word_count.keys()
sorted(ordered_keys)
print ("All the words and their frequency in", 'alice in wonderland')
for k in ordered_keys:
    print (k, word_count[k])

The Output here is just all of the words in the document NOT SORTED by
amount of occurrence.
I need help sorting this output of words in the Alice_in_wonderland.txt, as
well as help asking the user for the input information about the files.

If anyone could give me some guidance you will really be helping me out.

Please and Thank you
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140504/7fc740b3/attachment-0001.html>


More information about the Tutor mailing list