472,353 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

dict problem

Jon Clements wrote:
Alistair King wrote:
>Hi,

ive been trying to update a dictionary containing a molecular formula, but seem to be getting this error:
Traceback (most recent call last):
File "DS1excessH2O.py", line 242, in ?
updateDS1v(FCas, C, XDS)
NameError: name 'C' is not defined

dictionary is:

DS1v = {'C': 6, 'H': 10, 'O': 5}

#'Fxas' in each case will be integers but 'atoms' should be a float

def updateDS1v(Fxas, x, XDS):
while Fxas != 0:
atoms = DS1v.get('x') + Fxas*XDS
DS1v[x] = atoms

updateDS1v(FCas, C, XDS)
updateDS1v(FHas, H, XDS)
updateDS1v(FOas, O, XDS)
updateDS1v(FNas, N, XDS)
updateDS1v(FSas, S, XDS)
updateDS1v(FClas, Cl, XDS)
updateDS1v(FBras, Br, XDS)
updateDS1v(FZnas, Zn, XDS)
print DS1v

I know there is probably a simple solution but im quite new to python and am lost?



I strongly suggest reading through the tutorial.

I don't think there's enough code here for anyone to check it properly.
For instance, it looks like FCas exists somewhere as it's barfing on
trying to find C. Where is XDS defined etc...?

I can't see updateDS1v() ever completing: any Fxas passed in not equal
to 0 will repeat indefinately.

I'm guessing unless C is meant to be a variable, you mean to pass in
the string 'C'.

A dictionary already has it's own update method....

Perhaps if you explain what you're trying to do in plain English, we
could give you some pointers.

Jon.
sorry,

this has been a little rushed

XDS is defined before the function and is a float.
the Fxas values are also and they are integers
now ive tried

def updateDS1v(Fxas, x, XDS):
while Fxas != 0:
atoms = DS1v.get(x) + Fxas*XDS
DS1v['x'] = atoms

updateDS1v(FCas, 'C', XDS)
updateDS1v(FHas, H, XDS)
updateDS1v(FOas, O, XDS)
updateDS1v(FNas, N, XDS)
updateDS1v(FSas, S, XDS)
updateDS1v(FClas, Cl, XDS)
updateDS1v(FBras, Br, XDS)
updateDS1v(FZnas, Zn, XDS)
print DS1v

from this i get the error:

Traceback (most recent call last):
File "DS1excessH2O.py", line 242, in ?
updateDS1v(FCas, 'C', XDS)
File "DS1excessH2O.py", line 239, in updateDS1v
atoms = DS1v.get(x) + Fxas*XDS
TypeError: unsupported operand type(s) for +: 'int' and 'str'
with single quotes (FCas, 'C', XDS) to retrieve the value for that key
from the dictionary and then create the new value and replace the old value.

a
--
Dr. Alistair King
Research Chemist,
Laboratory of Organic Chemistry,
Department of Chemistry,
Faculty of Science
P.O. Box 55 (A.I. Virtasen aukio 1)
FIN-00014 University of Helsinki
Tel. +358 9 191 50392, Mobile +358 (0)50 5279446
Fax +358 9 191 50366

Oct 25 '06 #1
10 1395

Alistair King wrote:
Jon Clements wrote:
Alistair King wrote:
>
>
Hi,

ive been trying to update a dictionary containing a molecular formula, but seem to be getting this error:
Traceback (most recent call last):
File "DS1excessH2O.py", line 242, in ?
updateDS1v(FCas, C, XDS)
NameError: name 'C' is not defined

dictionary is:

DS1v = {'C': 6, 'H': 10, 'O': 5}

#'Fxas' in each case will be integers but 'atoms' should be a float

def updateDS1v(Fxas, x, XDS):
while Fxas != 0:
atoms = DS1v.get('x') + Fxas*XDS
DS1v[x] = atoms

updateDS1v(FCas, C, XDS)
updateDS1v(FHas, H, XDS)
updateDS1v(FOas, O, XDS)
updateDS1v(FNas, N, XDS)
updateDS1v(FSas, S, XDS)
updateDS1v(FClas, Cl, XDS)
updateDS1v(FBras, Br, XDS)
updateDS1v(FZnas, Zn, XDS)
print DS1v

I know there is probably a simple solution but im quite new to python and am lost?

>
I strongly suggest reading through the tutorial.
>
I don't think there's enough code here for anyone to check it properly.
For instance, it looks like FCas exists somewhere as it's barfing on
trying to find C. Where is XDS defined etc...?
>
I can't see updateDS1v() ever completing: any Fxas passed in not equal
to 0 will repeat indefinately.
>
I'm guessing unless C is meant to be a variable, you mean to pass in
the string 'C'.
>
A dictionary already has it's own update method....
>
Perhaps if you explain what you're trying to do in plain English, we
could give you some pointers.
>
Jon.
>
>
sorry,

this has been a little rushed

XDS is defined before the function and is a float.
the Fxas values are also and they are integers
now ive tried

def updateDS1v(Fxas, x, XDS):
while Fxas != 0:
atoms = DS1v.get(x) + Fxas*XDS
DS1v['x'] = atoms

updateDS1v(FCas, 'C', XDS)
updateDS1v(FHas, H, XDS)
updateDS1v(FOas, O, XDS)
updateDS1v(FNas, N, XDS)
updateDS1v(FSas, S, XDS)
updateDS1v(FClas, Cl, XDS)
updateDS1v(FBras, Br, XDS)
updateDS1v(FZnas, Zn, XDS)
print DS1v

from this i get the error:

Traceback (most recent call last):
File "DS1excessH2O.py", line 242, in ?
updateDS1v(FCas, 'C', XDS)
File "DS1excessH2O.py", line 239, in updateDS1v
atoms = DS1v.get(x) + Fxas*XDS
TypeError: unsupported operand type(s) for +: 'int' and 'str'
with single quotes (FCas, 'C', XDS) to retrieve the value for that key
from the dictionary and then create the new value and replace the old value.
One of Fxas or XDS is a string then...

Again, no-one can help you if we can't see what's actually there.

What are FCas, FHas etc... do they relate to the element? If so, isn't
that a dictionary in itself? And your update function is still an
infinite loop!

We're still in the dark as to what you're trying to do, try describing
something like: "for each element there is an associated 'F' value. For
each element in an existing molecule I wish to change the number of
'whatever' to be 'whatever' + my 'F' value * value XDS..."

Jon.

Oct 25 '06 #2
"Jon Clements" <jo****@googlemail.comwrites:
We're still in the dark as to what you're trying to do, try
describing something like: "for each element there is an associated
'F' value. For each element in an existing molecule I wish to change
the number of 'whatever' to be 'whatever' + my 'F' value * value
XDS..."
Even better, work on a minimal program to do nothing but reproduce the
unexpected behaviour. If you get to such a program and still don't
understand, then post it here so others can run it themselves and
explain.

--
\ "A cynic is a man who, when he smells flowers, looks around for |
`\ a coffin." -- Henry L. Mencken |
_o__) |
Ben Finney

Oct 25 '06 #3
Ben Finney wrote:
"Jon Clements" <jo****@googlemail.comwrites:

>We're still in the dark as to what you're trying to do, try
describing something like: "for each element there is an associated
'F' value. For each element in an existing molecule I wish to change
the number of 'whatever' to be 'whatever' + my 'F' value * value
XDS..."

Even better, work on a minimal program to do nothing but reproduce the
unexpected behaviour. If you get to such a program and still don't
understand, then post it here so others can run it themselves and
explain.

ive checked the values and XDS is actually returning a string where i
want a float ie '123.45' not 123.45.

--
Dr. Alistair King
Research Chemist,
Laboratory of Organic Chemistry,
Department of Chemistry,
Faculty of Science
P.O. Box 55 (A.I. Virtasen aukio 1)
FIN-00014 University of Helsinki
Tel. +358 9 191 50392, Mobile +358 (0)50 5279446
Fax +358 9 191 50366

Oct 25 '06 #4
Alistair King <al***********@helsinki.fiwrites:
Ben Finney wrote:
Even better, work on a minimal program to do nothing but reproduce
the unexpected behaviour. If you get to such a program and still
don't understand, then post it here so others can run it
themselves and explain.
ive checked the values and XDS is actually returning a string where i
want a float ie '123.45' not 123.45.
Can you please post a small, complete program that shows the behaviour
you want explained?

You get this program by writing it -- either by cutting away
irrelevant parts of the existing program, or (better) writing a new
program from scratch that does nothing except demonstrate the
behaviour.

Note that in the process of getting such a program behaving this way,
you may end up understanding the problem better.

--
\ "Democracy is the art of running the circus from the monkey |
`\ cage." -- Henry L. Mencken |
_o__) |
Ben Finney

Oct 25 '06 #5
Ben Finney wrote:
Alistair King <al***********@helsinki.fiwrites:

>Ben Finney wrote:
>>Even better, work on a minimal program to do nothing but reproduce
the unexpected behaviour. If you get to such a program and still
don't understand, then post it here so others can run it
themselves and explain.
ive checked the values and XDS is actually returning a string where i
want a float ie '123.45' not 123.45.

Can you please post a small, complete program that shows the behaviour
you want explained?

You get this program by writing it -- either by cutting away
irrelevant parts of the existing program, or (better) writing a new
program from scratch that does nothing except demonstrate the
behaviour.

Note that in the process of getting such a program behaving this way,
you may end up understanding the problem better.

i have seemed to work out most of the problems from the previous code,
now i have:
.................................................. .................................................. ................................................

heavy = raw_input("\n\n@@@@@@@@@@@@@@@@@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen, eg,
'C', 'N', 'S', 'Br'...: ")

print DSvalues

def updateDS1v(Fxas, x):
if Fxas != 0:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
DS1v[x] = atoms

updateDS1v(FCas, 'C')

print DS1v
.................................................. .................................................. ................................................

the problem now is converting badly formatted dictionary values into floats
ive been trying something like this, where 'value' is a typical entry
into the dictionary:
.................................................. .................................................. ................................................

IDLE 1.1
>>value = "'0.064250000000001084'"
print float(value)
and get the error, in IDLE and the code as:

Traceback (most recent call last):
File "<pyshell#3>", line 1, in -toplevel-
print float(value)
ValueError: invalid literal for float(): '0.064250000000001084'

.................................................. .................................................. ................................................

Is there any other way of removing double and single quotes from a
number, as a string, to give the float value again?

I know it would be easier to create a properly formatted dictionary
again and i will do that but it would be good to know as i want to get
this program running properly to get some results i need for work.
Everything else seems to be working fine but this.

thanks

a

--
Dr. Alistair King
Research Chemist,
Laboratory of Organic Chemistry,
Department of Chemistry,
Faculty of Science
P.O. Box 55 (A.I. Virtasen aukio 1)
FIN-00014 University of Helsinki
Tel. +358 9 191 50392, Mobile +358 (0)50 5279446
Fax +358 9 191 50366

Oct 30 '06 #6
Alistair King wrote:
Is there any other way of removing double and single quotes from a
number, as a string, to give the float value again?
help(str) describes what you can do with a string (an object of type
'str', that is). among the methods listed, you'll find:
| strip(...)
| S.strip([chars]) -string or unicode
|
| Return a copy of the string S with leading and trailing
| whitespace removed.
| If chars is given and not None, remove characters in chars instead.
| If chars is unicode, S will be converted to unicode before stripping
which looks like it should be pretty useful for this specific case:
>>value = "'0.064250000000001084'"
value
"'0.064250000000001084'"
>>value.strip("'")
'0.064250000000001084'
>>value.strip("'\"")
'0.064250000000001084'
>>float(value.strip("'\""))
0.064250000000001084

</F>

Oct 30 '06 #7
Fredrik Lundh wrote:
Alistair King wrote:

>Is there any other way of removing double and single quotes from a
number, as a string, to give the float value again?

help(str) describes what you can do with a string (an object of type
'str', that is). among the methods listed, you'll find:

> | strip(...)
| S.strip([chars]) -string or unicode
|
| Return a copy of the string S with leading and trailing
| whitespace removed.
| If chars is given and not None, remove characters in chars instead.
| If chars is unicode, S will be converted to unicode before stripping

which looks like it should be pretty useful for this specific case:
>>value = "'0.064250000000001084'"
>>value
"'0.064250000000001084'"
>>value.strip("'")
'0.064250000000001084'
>>value.strip("'\"")
'0.064250000000001084'
>>float(value.strip("'\""))
0.064250000000001084

</F>

Thanks..

the code works great now. I know these things are quite simple to learn
from books etc.. but i would be lost without this mailinglist, from lack
of time. Hopefully soon i can give something more complicated.
I ended up doing the dictionary formatting properly and the new code is:
.................................................. .................................................. ...........................
heavy = raw_input("\n\n@@@@@@@@@@@@@@@@@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen or
Hydrogen, ie, 'C', 'N', 'S', 'Br'...: ")

def updateDS1v(Fxas, x):
if Fxas !=0 and DS1v.get(x)!=None:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
else:
value = DSvalues.get(heavy)
floatvalue = float(value)
DS1v[x] = Fxas*floatvalue

updateDS1v(FCas, 'C')
updateDS1v(FHas, 'H')
updateDS1v(FOas, 'O')
updateDS1v(FNas, 'N')
updateDS1v(FSas, 'S')
updateDS1v(FClas, 'Cl')
updateDS1v(FBras, 'Br')
updateDS1v(FZnas, 'Zn')

.................................................. .................................................. ...........................
it works perfectly now

thanks for the help

a

--
Dr. Alistair King
Research Chemist,
Laboratory of Organic Chemistry,
Department of Chemistry,
Faculty of Science
P.O. Box 55 (A.I. Virtasen aukio 1)
FIN-00014 University of Helsinki
Tel. +358 9 191 50392, Mobile +358 (0)50 5279446
Fax +358 9 191 50366

Oct 30 '06 #8
Alistair King wrote:
the code works great now. I know these things are quite simple to learn
from books etc.. but i would be lost without this mailinglist, from lack
of time. Hopefully soon i can give something more complicated.
I ended up doing the dictionary formatting properly and the new code is:
heavy = raw_input("\n\n@@@@@@@@@@@@@@@@@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen or
Hydrogen, ie, 'C', 'N', 'S', 'Br'...: ")

def updateDS1v(Fxas, x):
if Fxas !=0 and DS1v.get(x)!=None:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
else:
value = DSvalues.get(heavy)
floatvalue = float(value)
DS1v[x] = Fxas*floatvalue

updateDS1v(FCas, 'C')
updateDS1v(FHas, 'H')
updateDS1v(FOas, 'O')
updateDS1v(FNas, 'N')
updateDS1v(FSas, 'S')
updateDS1v(FClas, 'Cl')
updateDS1v(FBras, 'Br')
updateDS1v(FZnas, 'Zn')
it works perfectly now
Probably not. Have you manually verified the result with more than one
example? Where does 'heavy' come from? Is that black hole 'atoms'
intentional?

# I'm just guessing here
for k, v in DSvalues.iteritems():
DSvalues[k] = float(v)

def updateDS1v(Fxas, x):
DS1v[x] = DS1v.get(x, 0) + Fxas*DSvalues[x]

Peter
Oct 30 '06 #9
Peter Otten wrote:
Alistair King wrote:

>the code works great now. I know these things are quite simple to learn
from books etc.. but i would be lost without this mailinglist, from lack
of time. Hopefully soon i can give something more complicated.
I ended up doing the dictionary formatting properly and the new code is:

>heavy = raw_input("\n\n@@@@@@@@@@@@@@@@@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen or
Hydrogen, ie, 'C', 'N', 'S', 'Br'...: ")

def updateDS1v(Fxas, x):
if Fxas !=0 and DS1v.get(x)!=None:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
else:
value = DSvalues.get(heavy)
floatvalue = float(value)
DS1v[x] = Fxas*floatvalue

updateDS1v(FCas, 'C')
updateDS1v(FHas, 'H')
updateDS1v(FOas, 'O')
updateDS1v(FNas, 'N')
updateDS1v(FSas, 'S')
updateDS1v(FClas, 'Cl')
updateDS1v(FBras, 'Br')
updateDS1v(FZnas, 'Zn')

>it works perfectly now

Probably not. Have you manually verified the result with more than one
example? Where does 'heavy' come from? Is that black hole 'atoms'
intentional?

# I'm just guessing here
for k, v in DSvalues.iteritems():
DSvalues[k] = float(v)

def updateDS1v(Fxas, x):
DS1v[x] = DS1v.get(x, 0) + Fxas*DSvalues[x]

Peter
yea...sorry i snipped one line by accident for the email
should be:

def updateDS1v(Fxas, x):
if Fxas !=0 and DS1v.get(x)!=None:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
DS1v[x] = atoms
else:
value = DSvalues.get(heavy)
floatvalue = float(value)
DS1v[x] = Fxas*floatvalue

thanks

a

--
Dr. Alistair King
Research Chemist,
Laboratory of Organic Chemistry,
Department of Chemistry,
Faculty of Science
P.O. Box 55 (A.I. Virtasen aukio 1)
FIN-00014 University of Helsinki
Tel. +358 9 191 50392, Mobile +358 (0)50 5279446
Fax +358 9 191 50366

Oct 30 '06 #10
Alistair King <al***********@helsinki.fiwrites:
heavy = raw_input("\n\n@@@@@@@@@@@@@@@@@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen, eg,
'C', 'N', 'S', 'Br'...: ")

print DSvalues

def updateDS1v(Fxas, x):
if Fxas != 0:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
DS1v[x] = atoms

updateDS1v(FCas, 'C')

print DS1v
I see from later posts that you have found solutions to your current
issues.

Just a note on what you posted: Please make sure that you post your
message as plain text. As you can see from what I quoted above, the
indentation is completely lost in the code you posted.

--
\ "We cannot solve our problems with the same thinking we used |
`\ when we created them." -- Albert Einstein |
_o__) |
Ben Finney

Oct 30 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a...
2
by: GrelEns | last post by:
hello, i would like if this behaviour can be obtained from python : trap an attributeError from inside a subclassing dict class... (here is a...
1
by: Alexander Kervero | last post by:
Hi ,today i was reading diveinto python book,in chapter 5 it has a very generic module to get file information,html,mp3s ,etc. The code of the...
4
by: Steven Bethard | last post by:
I'd like to subclass dict to disallow overwriting of keys, something like: >>> class SafeDict(dict): .... def __setitem__(self, key, value):...
5
by: Randy Bush | last post by:
a dict written as pKey = (prefix, pLen, origin) val = dict.get(pKey) if val == None: dict = (timeB, timeB) else: if val > timeB: val =...
3
by: Bengt Richter | last post by:
Has anyone found a way besides not deriving from dict? Shouldn't there be a way? TIA (need this for what I hope is an improvement on the...
15
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword...
3
by: aking | last post by:
Dear Python people, im a newbie to python and here...so hello! Im trying to iterate through values in a dictionary so i can find the closest...
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some...
8
by: james_027 | last post by:
hi for example I have this dictionary dict = {'name':'james', 'language':'english'} value = 'sex' in dict and dict or 'unknown' is a...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.