Re: Semi-Newbie needs a little help
Thanks all for your help. I appreciate it. The problem was in the function. A simple bug which I should have caught but I had my mental blinders on and was sure the problem was outside the function. The answers have given me a lot to learn so thanks for that as well. -- http://mail.python.org/mailman/listinfo/python-list
Re: Semi-Newbie needs a little help
> Nile (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 URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: p...@vanoostrum.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Semi-Newbie needs a little help
En Mon, 06 Jul 2009 19:49:41 -0300, MRAB escribió: Chris Rebert wrote: from collections import defaultdict counts = defaultdict(lambda: 0) Better is: counts = defaultdict(int) For speed? This is even faster: zerogen = itertools.repeat(0).next counts = defaultdict(zerogen) -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Semi-Newbie needs a little help
MRAB wrote: 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. 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Semi-Newbie needs a little help
On Tue, 07 Jul 2009 00:29:36 +0100, Nile wrote: 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 Have you checked the indentation of the print statement that produces this? Is it perhaps inside a loop still? -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: Semi-Newbie needs a little help
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Semi-Newbie needs a little help
On Jul 6, 5:22 pm, Chris Rebert wrote: > On Mon, Jul 6, 2009 at 3:02 PM, Nile 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Semi-Newbie needs a little help
On Jul 6, 5:30 pm, "Pablo Torres N." wrote: > On Mon, Jul 6, 2009 at 17:02, Nile wrote: > > 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) > > Correcting your indentation errors and moving your comments above the > line they reference will attract more help from others in this list > ;-) > > Also, I'd recommend limiting your line length to 80 chars, since lines > are wrapped anyway. > > -- > Pablo Torres N. Yup - Sorry, first post ever - next ones will be better formatted -- http://mail.python.org/mailman/listinfo/python-list
Re: Semi-Newbie needs a little help
Chris Rebert wrote: On Mon, Jul 6, 2009 at 3:02 PM, Nile 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
Re: Semi-Newbie needs a little help
On Mon, Jul 6, 2009 at 17:02, Nile wrote: > 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) Correcting your indentation errors and moving your comments above the line they reference will attract more help from others in this list ;-) Also, I'd recommend limiting your line length to 80 chars, since lines are wrapped anyway. -- Pablo Torres N. -- http://mail.python.org/mailman/listinfo/python-list
Re: Semi-Newbie needs a little help
On Mon, Jul 6, 2009 at 3:02 PM, Nile 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) 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 -- http://mail.python.org/mailman/listinfo/python-list