Idenfity numbers in variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alfons Nonell-Canals

    Idenfity numbers in variables

    Hello,
    I have a trouble and I don't know how to solve it. I am working with
    molecules and each molecule has a number of atoms. I obtain each atom
    spliting the molecule.

    Ok. It is fine and I have no problem with it.

    The problem is when I have to work with these atoms. These atoms usually
    are only a letter but, sometimes it can also contain one o more numbers.
    If they contein a number I have to manipulate them separately.

    If the number was allways the same I know how to identify them, for
    example, 1:

    atom = 'C1'

    if '1' in atom:
    print 'kk'

    But, how can I do to identify in '1' all possibilities from 1-9, I
    tried:

    if '[1-9]', \d,...

    Any comments, please?

    Regards,

    Alfons.


    --
    ------------
    Alfons Nonell-Canals, PhD
    Chemogenomics Lab
    Research Group on Biomedical Informatics (GRIB) - IMIM/UPF
    Parc de Recerca Biomèdica de Barcelona (PRBB)
    C/ Doctor Aiguader, 88 - 08003 Barcelona
    alfons.nonell@u pf.edu - http://cgl.imim.es




  • John Machin

    #2
    Re: Idenfity numbers in variables

    On Oct 20, 10:16 pm, Alfons Nonell-Canals <alfons.non...@ upf.edu>
    wrote:
    Hello,
    I have a trouble and I don't know how to solve it. I am working with
    molecules and each molecule has a number of atoms. I obtain each atom
    spliting the molecule.
    >
    Ok. It is fine and I have no problem with it.
    >
    The problem is when I have to work with these atoms. These atoms usually
    are only a letter but, sometimes it can also contain one o more numbers.
    If they contein a number I have to manipulate them separately.
    >
    If the number was allways the same I know how to identify them, for
    example, 1:
    >
    atom = 'C1'
    >
    if '1' in atom:
            print 'kk'
    >
    But, how can I do to identify in '1' all possibilities from 1-9, I
    tried:
    >
    if '[1-9]', \d,...
    >
    Any comments, please?
    Sorry, I can't parse "identify in '1' all possibilities from 1-9".

    Please give examples of what a valid atom with more than one digit
    looks like, and what is not valid e.g. 'C12' is valid, so is 'C21',
    but 'C11' and 'C22' are not valid.

    Then give examples (in words, not in pseudo-Python) of tests/queries
    that should produce True, and examples that should produce False -- if
    indeed the result is intended to be a Boolean; if not, you'd better
    tell us what you want.

    Cheers,
    John

    Comment

    • Peter Otten

      #3
      Re: Idenfity numbers in variables

      Alfons Nonell-Canals wrote:
      I have a trouble and I don't know how to solve it. I am working with
      molecules and each molecule has a number of atoms. I obtain each atom
      spliting the molecule.
      >
      Ok. It is fine and I have no problem with it.
      >
      The problem is when I have to work with these atoms. These atoms usually
      are only a letter but, sometimes it can also contain one o more numbers.
      If they contein a number I have to manipulate them separately.
      >
      If the number was allways the same I know how to identify them, for
      example, 1:
      >
      atom = 'C1'
      >
      if '1' in atom:
      print 'kk'
      >
      But, how can I do to identify in '1' all possibilities from 1-9, I
      tried:
      >
      if '[1-9]', \d,...
      >
      Any comments, please?


      Peter

      Comment

      • Paul McGuire

        #4
        Re: Idenfity numbers in variables

        On Oct 20, 7:07 am, Peter Otten <__pete...@web. dewrote:
        Alfons Nonell-Canals wrote:
        I have a trouble and I don't know how to solve it. I am working with
        molecules and each molecule has a number of atoms. I obtain each atom
        spliting the molecule.
        >
        Ok. It is fine and I have no problem with it.
        >
        The problem is when I have to work with these atoms. These atoms usually
        are only a letter but, sometimes it can also contain one o more numbers..
        If they contein a number I have to manipulate them separately.
        >
        If the number was allways the same I know how to identify them, for
        example, 1:
        >
        atom = 'C1'
        >
        if '1' in atom:
        print 'kk'
        >
        But, how can I do to identify in '1' all possibilities from 1-9, I
        tried:
        >
        if '[1-9]', \d,...
        >
        Any comments, please?
        >

        >
        Peter- Hide quoted text -
        >
        - Show quoted text -
        Wow, that sure is a lot of code. And I'm not sure the OP wants to
        delve into re's just to solve this problem. Here is the pyparsing
        rendition (although it does not handle the recursive computation of
        submolecules given in parens, as the Tim Peters link above does):


        The pyparsing version defines chemical symbols and their coefficients
        as using the following code:

        caps = "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
        lowers = caps.lower()
        digits = "0123456789 "

        element = Word( caps, lowers )
        integer = Word( digits )
        elementRef = Group( element + Optional( integer, default="1" ) )
        chemicalFormula = OneOrMore( elementRef )


        Then to parse a formula like C6H5OH, there is no need to code up a
        tokenizer, just call parseString:

        elements = chemicalFormula .parseString("C 6H5OH")

        The URL above links to a better annotated example, included 2 more
        extended versions that show how to use the resulting parsed data to
        compute the molecular weight of the chemical.

        -- Paul

        Comment

        Working...