0

Count how many words there are in the .txt file.

Then, print the words ordered by frequency and alphabetically.

def count_words():
    d = dict()
    word_file = open('words.txt')
    for line in word_file:
        word = line.strip();
        d = countwords(word,d)
    return d

I'm not sure if I am doing this correctly. Hoping someone can help me out.

When I run the program, I receive:

>>>
>>>

It's a paragraph of a speech.

zero323
  • 322,348
  • 103
  • 959
  • 935
Joe21
  • 45
  • 1
  • 2
  • 11
  • "By frequency and alphabetically" - wonder if this could be done using a single sort call. – MxLDevs Nov 14 '13 at 03:53
  • 1
    that doesnt make sense. Do you want the word with the most keys printed first or the words in alphabetical? If you sort them by their counts then you could only sort alphabetically for ties. I think if you need to sort alphabetically by ties, you'll need to manually build a list from the d dict after it is constructed instead of calling my print statement below. – Tommy Nov 14 '13 at 03:56

2 Answers2

2

I would use a dictionary like you are but differently:

def count_words():
d = dict()
word_file = open('words.txt')
for line in word_file:
    word = line.strip();
    if word not in d.keys():
        d[word] = 0
    d[word]+=1

Then you can sort the keys by their counts and print them:

from operator import itemgetter
print(sorted(d.items(), key=itemgetter(1)))

For the sorting blurb I used: Sort a Python dictionary by value

Also, your program doesnt have any print statements, just a return line, which is why you get nothing.

Community
  • 1
  • 1
Tommy
  • 12,588
  • 14
  • 59
  • 110
  • you probably want to remove punctuation, then also split each line into words according to spaces. this answer assumes that each word is on its own line. – adityajones Nov 14 '13 at 03:56
  • Well I just copied the OPs code, which also assumes each word on its own line. – Tommy Nov 14 '13 at 03:57
  • no complaint. just a comment for OP to ponder on if he does use your solution (which does work). – adityajones Nov 14 '13 at 04:08
  • So, wait you have them separated, would I put them together in my script? The print statement you have there? I did that and it still returned nothing. – Joe21 Nov 14 '13 at 05:08
1
#!/usr/local/cpython-3.3/bin/python

import pprint
import collections

def words(filename):
    with open(filename, 'r') as file_:
        for line in file_:
            for word in line.split():
                yield word.lower()

counter = collections.Counter(words('/etc/services'))

sorted_by_frequency = sorted((value, key) for key, value in counter.items())
sorted_by_frequency.reverse()
print('Sorted by frequency')
pprint.pprint(sorted_by_frequency)
print('')

sorted_alphabetically = sorted((key, value) for key, value in counter.items())
print('Sorted alphabetically')
pprint.pprint(sorted_alphabetically)
dstromberg
  • 6,954
  • 1
  • 26
  • 27