So this is my teachers attempt to ease me into Python, a simple program to read in a text file, dump the contents into a dictionary, and then either sort it and print it, or print the first n items in the dictionary based on a command line argument.
I've got a fair amount figured out so far, with quite a ways to go.
I have the dictionary organized by {word:Number_of_occurrences}. Right now my program will dump the value portion, namely the occurrences, but not the pair with it, and looking for a function within dictionary to get this doesn't seem like anything of what I want.
Also, first n hates me.
import sys
if len(sys.argv) > 1:
n = sys.argv[1] #takes command line argument for out value of n and assigns it to the variable n
else:
n = None
input_file = open("input.txt") #open input file
try:
input = input_file.read() #read file into one long string, input
finally:
input_file.close() #close the file
D = {} #create empty dictionary
word_list = input.split()
#.strip(';,.?!\/<>-:"@#$%^&(`)+*/+_') #create list of words, stripped of punctuations
for word in word_list:
if D.has_key(word): #check if word(key) is in dictionary
val = D.get(word) #get value from word
D[word] = (val + 1) #increment val (word count) and readd to the dictionary with new word count
else:
D[word] = 1 #otherwise add the new word to the dictionary with value (wordcount) 1
output_file = open("output.txt") #open file for writing output
def printDict(D, n):
"""Either takes an argument n and prints the first n terms in the dictionary,
or sorts the dictionary and prints the contents."""
keys = D.keys()
if n == None: #args value of n never given, sorting dict. and printing it all
keys.sort() #sorts the list of keys
for key in keys:
print D[key]
#output_file.writelines(D[key] for key in keys)
else: #args value of n given, printing first n terms
i = 0
while i < n:
print D[keys[i]]
i = i + 1
#output_file.writelines(D[key] for key in keys)
printDict(D, n)
So the first n starts fine, and then i just runs away, only to be seen again when the program crashes. I've stepped through, and despite the i < n, it just keeps going.
So, any help and general feedback would be nice.
Posts
See how many books I've read so far in 2010
The strip call should be done in a list comprehension since you want a list of words with the punctuation removed.
Which will return a new list after strip() is called on every word in word_list.
Instead of
you should use:
Instead of
use
Instead of trying to do a manual loop use a for loop with range():
range() with a single argument will return a list of integers whose length is equal to n. You can also specify the start and step values.
It isn't printing the key,value pairs because your print statement is only printing the value (D[key]) and not key as well. For example:
See how many books I've read so far in 2010