dict problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alistair King

    #1

    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.p y", 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(FCa s, C, XDS)
    >updateDS1v(FHa s, H, XDS)
    >updateDS1v(FOa s, O, XDS)
    >updateDS1v(FNa s, N, XDS)
    >updateDS1v(FSa s, S, XDS)
    >updateDS1v(FCl as, Cl, XDS)
    >updateDS1v(FBr as, Br, XDS)
    >updateDS1v(FZn as, 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(FCla s, Cl, XDS)
    updateDS1v(FBra s, Br, XDS)
    updateDS1v(FZna s, Zn, XDS)
    print DS1v

    from this i get the error:

    Traceback (most recent call last):
    File "DS1excessH2O.p y", line 242, in ?
    updateDS1v(FCas , 'C', XDS)
    File "DS1excessH2O.p y", 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

  • Jon Clements

    #2
    Re: dict problem


    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.p y", 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(FCla s, Cl, XDS)
    updateDS1v(FBra s, Br, XDS)
    updateDS1v(FZna s, 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(FCla s, Cl, XDS)
    updateDS1v(FBra s, Br, XDS)
    updateDS1v(FZna s, Zn, XDS)
    print DS1v
    >
    from this i get the error:
    >
    Traceback (most recent call last):
    File "DS1excessH2O.p y", line 242, in ?
    updateDS1v(FCas , 'C', XDS)
    File "DS1excessH2O.p y", 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.

    Comment

    • Ben Finney

      #3
      Re: dict problem

      "Jon Clements" <joncle@googlem ail.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

      Comment

      • Alistair King

        #4
        Re: dict problem

        Ben Finney wrote:
        "Jon Clements" <joncle@googlem ail.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

        Comment

        • Ben Finney

          #5
          Re: dict problem

          Alistair King <alistair.king@ helsinki.fiwrit es:
          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

          Comment

          • Alistair King

            #6
            dict problem

            Ben Finney wrote:
            Alistair King <alistair.king@ helsinki.fiwrit es:
            >
            >
            >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(he avy)
            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.06425000000 0001084'"
            >>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.064250000000 001084'

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

            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

            Comment

            • Fredrik Lundh

              #7
              Re: dict problem

              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.06425000000 0001084'"
              >>value
              "'0.06425000000 0001084'"
              >>value.strip(" '")
              '0.064250000000 001084'
              >>value.strip(" '\"")
              '0.064250000000 001084'
              >>float(value.s trip("'\""))
              0.0642500000000 01084

              </F>

              Comment

              • Alistair King

                #8
                Re: dict problem

                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.06425000000 0001084'"
                >>value
                "'0.06425000000 0001084'"
                >>value.strip(" '")
                '0.064250000000 001084'
                >>value.strip(" '\"")
                '0.064250000000 001084'
                >>float(value.s trip("'\""))
                0.0642500000000 01084
                >
                </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)!=No ne:
                value = DSvalues.get(he avy)
                floatvalue = float(value)
                atoms = DS1v.get(x) + Fxas*floatvalue
                else:
                value = DSvalues.get(he avy)
                floatvalue = float(value)
                DS1v[x] = Fxas*floatvalue

                updateDS1v(FCas , 'C')
                updateDS1v(FHas , 'H')
                updateDS1v(FOas , 'O')
                updateDS1v(FNas , 'N')
                updateDS1v(FSas , 'S')
                updateDS1v(FCla s, 'Cl')
                updateDS1v(FBra s, 'Br')
                updateDS1v(FZna s, '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

                Comment

                • Peter Otten

                  #9
                  Re: dict problem

                  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)!=No ne:
                  value = DSvalues.get(he avy)
                  floatvalue = float(value)
                  atoms = DS1v.get(x) + Fxas*floatvalue
                  else:
                  value = DSvalues.get(he avy)
                  floatvalue = float(value)
                  DS1v[x] = Fxas*floatvalue
                  >
                  updateDS1v(FCas , 'C')
                  updateDS1v(FHas , 'H')
                  updateDS1v(FOas , 'O')
                  updateDS1v(FNas , 'N')
                  updateDS1v(FSas , 'S')
                  updateDS1v(FCla s, 'Cl')
                  updateDS1v(FBra s, 'Br')
                  updateDS1v(FZna s, '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.iterit ems():
                  DSvalues[k] = float(v)

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

                  Peter

                  Comment

                  • Alistair King

                    #10
                    Re: dict problem

                    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)!=No ne:
                    >value = DSvalues.get(he avy)
                    >floatvalue = float(value)
                    >atoms = DS1v.get(x) + Fxas*floatvalue
                    >else:
                    >value = DSvalues.get(he avy)
                    >floatvalue = float(value)
                    >DS1v[x] = Fxas*floatvalue
                    >>
                    >updateDS1v(FCa s, 'C')
                    >updateDS1v(FHa s, 'H')
                    >updateDS1v(FOa s, 'O')
                    >updateDS1v(FNa s, 'N')
                    >updateDS1v(FSa s, 'S')
                    >updateDS1v(FCl as, 'Cl')
                    >updateDS1v(FBr as, 'Br')
                    >updateDS1v(FZn as, '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.iterit ems():
                    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)!=No ne:
                    value = DSvalues.get(he avy)
                    floatvalue = float(value)
                    atoms = DS1v.get(x) + Fxas*floatvalue
                    DS1v[x] = atoms
                    else:
                    value = DSvalues.get(he avy)
                    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

                    Comment

                    • Ben Finney

                      #11
                      Re: dict problem

                      Alistair King <alistair.king@ helsinki.fiwrit es:
                      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(he avy)
                      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

                      Comment

                      Working...