Semi-Newbie needs a little help
Piet van Oostrum
piet at cs.uu.nl
Tue Jul 7 04:01:40 EDT 2009
>>>>> Nile <nile_mcadams at yahoo.com> (N) wrote:
>N> I initialized the dictionary earlier in the program like this -
>N> hashtable = {}
>N> I changed the "dict" to hashtable but I still get the same result
>N> I will try to learn about the defaultdict but I'm just trying to keep
>N> it as simple as I can for now
>N> Revised code
>N> for x in range(len(file_list)):
>N> d = open(file_list[x] , "r")
>N> data = d.readlines()
>N> k = 0
>N> k = above_or_below(data)
>N> print "here is the value that was returned ",k
>N> hashtable[k] = hashtable.get(k,0) + 1
>N> hashtable_list = hashtable.values()
>N> print "here is a list of the dictionary values ", hashtable_list
>N> print "the length of the dictionary is ", len(hashtable)
>N> Output
>N> # The first 3 lines are printed from the function
>N> # right before the return statement. This output
>N> # snippet shows the last two stocks. The function
>N> # SAYS it is returning the correct value but only
>N> # the last date seems to make it to the hashtable
>N> Function will return k which = 11/11/2008
>N> Function will return k which = 11/12/2008
>N> Function will return k which = 11/14/2008
>N> # this line is printed from the code above
>N> # I don't understand why all three dates don't
>N> # seem to make it to the main program. Only
>N> # the last date seems to be recognized
>N> here is the value that was returned 11/14/2008
>N> Function will return k which = 11/11/2008
>N> Function will return k which = 11/12/2008
>N> Function will return k which = 11/14/2008
>N> here is the value that was returned 11/14/2008
>N> here is a list of the dictionary values [5]
>N> the length of the dictionary is 1
>>> Exit code: 0
Now in your code there is a 1-1 relation between printing
"here is the value that was returned" and incrementing the hashtable
entry. In your log there are only 2 prints of "here is the value that
was returned" so how can the count be 5? Are you hiding something from
us?
--
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org
More information about the Python-list
mailing list