Semi-Newbie needs a little help

Nile nile_mcadams at yahoo.com
Mon Jul 6 18:02:45 EDT 2009


I am trying to write a simple little program to do some elementary
stock market analysis.  I read lines, send each line to a function and
then the function returns a date which serves as a key to a
dictionary. Each time a date is returned I want to increment the value
associated with that date. The function seems to be working properly.
By means of a print statement I have inserted just before the return
value I can see there are three dates that are returned which is
correct.  The dictionary only seems to capture the last date. My test
data consists of five stocks, each stock with five days. The correct
answer would be a count of 5 for the second day, the third day, and
the last day -- 11/14/2008.

Here is the a code, followed by a portion of the output.  I know
enough to write simple little programs like this with no problems up
until now but I don't know enough to figure out what I am doing
wrong.

Code

    for x in range(len(file_list)):
    d = open(file_list[x] , "r")
    data = d.readlines()
    k = above_or_below(data)                                # This
function seems to work correctly
    print "here is the value that was returned " , k
    dict[k] = dict.get(k,0) + 1

    dict_list = dict.values()
    print "here is a list of the dictionary values ", dict_list
    print "the length of the dictionary is ", len(dict)

And here is some output
    Function will return k which = 11/11/2008   #  These 3 lines are
printed from the function just before the return
    Function will return k which = 11/12/2008   #   This sample shows
stocks 4 and 5 but 1,2,3 are the same.
    Function will return k which = 11/14/2008
    here is the value that was returned 11/14/2008     # printed from
code above  - only the last day seems to be
    Function will return k which = 11/11/2008             #
recognized.
    Function will return k which = 11/12/2008
    Function will return k which = 11/14/2008
    here is the value that was returned 11/14/2008
    here is a list of the dictionary values [5]             # dict has
counted only the last day for 5 stocks
    the length of the dictionary is 1
    >Exit code: 0






More information about the Python-list mailing list