Our new Indie Games subforum is now open for business in G&T. Go and check it out, you might land a code for a free game. If you're developing an indie game and want to post about it, follow these directions. If you don't, he'll break your legs! Hahaha! Seriously though.
Our rules have been updated and given their own forum. Go and look at them! They are nice, and there may be new ones that you didn't know about! Hooray for rules! Hooray for The System! Hooray for Conforming!
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.
Also, it is better coding practice in that very first bit to define "n = None" before the part where you assign it to the command line param, then you can leave off the else statement.
And you don't just get $5 off used games.
WKC is $59.99 New. Used is $34.99.
SO is $64.99 new used is $34.99.
Eternal Sonatra new is $34.99 used is $17.99.
You get a savings of 50% or more if your buying used.
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.
[word.strip(';,.?!\/<>-:"@#$&#37;^&(`)+*/+_') for word in word_list]Which will return a new list after strip() is called on every word in word_list.
Instead of
if D.has_key(word): val = D.get(word)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:
>>> D = {"One": 1, "Two": 2, "Three": 3} >>> keys = sorted(D.keys()) >>> keys ['One', 'Three', 'Two'] >>> for k in keys: ... print("Key: %s\t Value: %s" % (k, D[k])) ... Key: One Value: 1 Key: Three Value: 3 Key: Two Value: 2See how many books I've read so far in 2010