The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Some Python help

clsCorwinclsCorwin Registered User regular
edited March 2009 in Help / Advice Forum
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.

clsCorwin on

Posts

  • clsCorwinclsCorwin Registered User regular
    edited March 2009
    Oh, since I just noticed it, where should that .strip() go to get rid of all that punctuation?

    clsCorwin on
  • BarrakkethBarrakketh Registered User regular
    edited March 2009
    Style issues...where to start...

    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:
    if word in D:
    val = D[word] #which you use in the next few lines
    

    Instead of
    if n == None:
    

    use
    if n is None:
    

    Instead of trying to do a manual loop use a for loop with range():
    for i in range(n):
    

    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: 2
    

    Barrakketh on
    Rollers are red, chargers are blue....omae wa mou shindeiru
  • DocDoc Registered User, ClubPA regular
    edited March 2009
    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.

    Doc on
  • clsCorwinclsCorwin Registered User regular
    edited March 2009
    this is true. Which is weird, cuz in C++ I would have done that, lol.

    clsCorwin on
Sign In or Register to comment.