Semi-Newbie needs a little help

Dave Angel davea at ieee.org
Mon Jul 6 21:18:57 EDT 2009


MRAB wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Nile wrote:
> [snip]
>> 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()
>
> What's the point of the following line?
>
>>     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
>
> I think there's a bug in 'above_or_below' which you haven't noticed.
>
> </div>
>
The code supplied doesn't match the output supplied.  It'd probably help 
if the output was actually pasted from the command window, instead of 
retyped with more comments than data.  And of course it'd help if you 
actually showed us the function above_or_below(), which is probably 
where the bug is.  If it prints three values, but returns a string, then 
why would you be surprised?   Maybe you intended it to return a list?

Each time above_or_below() it's called, it prints three lines before 
returning, but only returns the final value.  How big is file_list?  I 
suspect it's of length 5.

And the output is shown as repeated twice, but it probably was actually 
five sets of data.

You do know you can print hashtable, don't you?  You're extracting and 
printing the values, but not bothering with the keys.

I suggest you add a print to the entry point of above_or_below(), to 
match the one you have for its return.  And all of these print lines 
should be indented.  That might make it easier to interpret the output, 
without lots of inserted comments.

DaveA



More information about the Python-list mailing list