Intro
Programming
with
Python

Spring 2018
course
site

April 11

We went over

We worked through a program to count words in a file and got nearly to the end of the task. (The last bit would be to sort the words in reverse order of frequency.)

Here's where we were at the end of class.

words.txt is

cat
dog
elephant
cat
cat
mouse
mouse
duck
mouse
duck
dog

"""
 count_words.py

 Read words.txt which has one word per line.
 Count how many times each word appears
 and print a summary.

 Tested with python 2.7.

   $ python count_words.py
   -- count_words --
     12 cat          
      2 dog          
      2 duck         
      1 elephant     
      3 mouse        
"""

def get_words(filename):
    """ return a list of words from a file with one word per line """
    # file = open(filename)
    # words = file.read()      # get file as string
    # no_newlines = words.split('\n')  # split at newlines
    # no_blanks = filter(None, no_blanks)  # remove ''
    # return no_blanks
    # ... or all of that in one expression.
    return filter(None, open(filename).read().split('\n'))

def count(words):
    """ Return a dictionary of { word : how_many_times } """
    how_many = {}
    for word in words:
        how_many[word] = how_many.get(word, 0) + 1
    return how_many

def main():
    print '-- count_words --'
    words = get_words('words.txt')
    print "words = {}".format(words)
    counts = count(words)
    print "counts = {}".format(counts)

if __name__ == '__main__':
    import doctest
    doctest.testmod()
    main()
https://cs.marlboro.college /cours /spring2018 /python /notes /apr11
last modified Sun December 22 2024 4:57 am