473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1463

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 variable length list of associated values after...
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 silly examples to explain my question) class...
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 example is here :...
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): .... if key in self: .... raise...
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 = timeB if val < timeB: val = timeB
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 Larosa/Foord OrderedDict ;-) I guess I can just document...
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 arguments (e.g a default value or an orderby...
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 value and then extract the key for that...
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 key/value pairs. For this I thought I should override...
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 right pythonic of doing this one? I am trying to get a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.