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.
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)
And here is some output
Function will return k which = 11/11/2008 # These 3 lines are
printed from the function just before the return
Function will return k which = 11/12/2008 # This sample shows
stocks 4 and 5 but 1,2,3 are the same.
Function will return k which = 11/14/2008
here is the value that was returned 11/14/2008 # printed from
code above - only the last day seems to be
Function will return k which = 11/11/2008 #
recognized.
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] # dict has
counted only the last day for 5 stocks
the length of the dictionary is 1
>Exit code: 0
> 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
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.
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.
Yup - Sorry, first post ever - next ones will be better formatted
If I'd like to set dielectric constant for the certain material, is it possible to do such in Python environment? If yes, how to do or what syntax can be used?
Also, I'd like to get a simulation result, like voltage, is it possible to get this value in Python environment?
Please let me know,
nacim
Quite possibly, however you're going to have to give us a *lot* more
information before the answers you get will be worth anything at all.
How is "the certain material" represented? What simulator are you
using? Which Python environment? Which Python version for that matter?
We may appear to be mind-readers, but we usually need a bit more
than this to work on.
--
Rhodri James *-* Wildebeest Herder to the Masses
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
What's the point of the following line?
I think there's a bug in 'above_or_below' which you haven't noticed.
> 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?
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
> Dear Python gurus,
>
> If I'd like to set dielectric constant for the certain material, is it
> possible to do such in Python environment? If yes, how to do or what
> syntax can be used?
certain_material.dielectric_constant = 1.234
> Also, I'd like to get a simulation result, like voltage, is it possible
> to get this value in Python environment?
Yes.
--
Steven
>> 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
>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 <pi...@cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: pi...@vanoostrum.org
The answers to your first and third questions are, "yes" and "yes". :]
(Generally speaking if something can be done by a computer it can be
done with python.)
As for your second question check out the "magnitude" package:
http://pypi.python.org/pypi/magnitude/ and
http://juanreyero.com/magnitude/
(That second link also has links to three other packages that deal
with units of measurement.)
Ii has units for the SI measurements, including volts and coulombs, so
you should be able to accomplish your goals with it.
The tricky thing is, as far as I can tell from the wikipedia entry
(http://en.wikipedia.org/wiki/Relative_static_permittivity),
"dielectric constant" seems to be a dimensionless number, i.e. C/C...
I could be totally daft though.
HTH,
~Simon
Thank you for trying to help to my initial and not well written questions. I will compile more detailed information and ask again. Btw, I am giving a glimpse to: "How To Ask Questions The Smart Way".
nacim
Give this one a try too: http://www.mikeash.com/getting_answers.html
It doesn't talk down to you...as much :P
--
Pablo Torres N.
Nice! I'll try remembering that one.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/
"as long as we like the same operating system, things are cool." --piranha