Semi-Newbie needs a little help

Nile nile_mcadams at yahoo.com
Tue Jul 7 01:29:36 CEST 2009


On Jul 6, 5:22 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Mon, Jul 6, 2009 at 3:02 PM, Nile<nile_mcad... at 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.defau...)
> 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)
>
> 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.
>
> Cheers,
> Chris
> --http://blog.rebertia.com- Hide quoted text -
>
> - Show quoted text -

I initialized the dictionary earlier in the program like this -

  hashtable = {}

I changed the "dict" to hashtable but I still get the same result
I will try to learn about the defaultdict but I'm just trying to keep
it as simple as I can for now

Revised code

for x in range(len(file_list)):
	d = open(file_list[x] , "r")
	data = d.readlines()
	k = 0
	k = above_or_below(data)
	print "here is the value that was returned ",k
	hashtable[k] = hashtable.get(k,0) + 1


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

Output
# The first 3 lines are printed from the function
# right before the return statement.  This output
# snippet shows the last two stocks.  The function
# SAYS it is returning the correct value but only
# the last date seems to make it to the hashtable
Function will return k which = 11/11/2008
Function will return k which = 11/12/2008
Function will return k which = 11/14/2008

# this line is printed from the code above
# I don't understand why all three dates don't
# seem to make it to the main program.  Only
# the last date seems to be recognized
here is the value that was returned 11/14/2008

Function will return k which = 11/11/2008
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]
the length of the dictionary is 1
>Exit code: 0



More information about the Python-list mailing list