[Tutor] Alice_in_wonderland Question
C Smith
illusiontechniques at gmail.com
Mon May 5 03:45:41 CEST 2014
>ordered_keys = word_count.keys()
>sorted(ordered_keys)
sorted() does not modify the list, but returns a sorted version of the
list for me on Python 2.7
my_sorted_list = sorted(ordered_keys)
This will alphabetize all of the words, regardless of frequency.
>print ("All the words and their frequency in", 'alice in wonderland')
>for k in ordered_keys:
for k in my_sorted_keys:
> print (k, word_count[k])
This will still print out all the words alphabetically, regardless of frequency.
Maybe you could have a different dictionary for each value, for
example, a dictionary of all the words that appear once, twice, three
times.
By 'asking the user for input information' do you mean so they could
pass any txt file into the program to be sorted?
On Sun, May 4, 2014 at 5:04 PM, Jake Blank <jakenblank at gmail.com> wrote:
> 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
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list