Chris Rebert wrote:
On Mon, Jul 6, 2009 at 3:02 PM, Nile<nile_mcad...@yahoo.com> wrote:
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.

   for x in range(len(file_list)):

for filename in file_list:
#I'm assuming the lack of indentation on the subsequent lines is a
mere transcription error...

   d = open(file_list[x] , "r")

    d = open(filename , "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` is the name of a builtin type. Please rename this variable to
avoid shadowing the type.
Also, where is this variable even initialized? It's not in this code
snippet you gave.
Further, I would recommend using a defaultdict
(http://docs.python.org/dev/library/collections.html#collections.defaultdict)
rather than a regular dictionary; this would make the
count-incrementing part nicer.

Taking these changes into account, your code becomes:

from collections import defaultdict

counts = defaultdict(lambda: 0)

Better is:

counts = defaultdict(int)

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

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


I don't immediately see what's causing your problem, but guess that it
might've be related to the initialization of the `dict` variable.

It might be that the indentation was wrong where the count is
incremented, but I can't tell because none of the lines were shown
indented.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to