Idenfity numbers in variables

  • Thread starter Alfons Nonell-Canals
  • Start date
A

Alfons Nonell-Canals

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
(e-mail address removed) - http://cgl.imim.es

http://alfons.elmeuportal.cat
http://www.selenocisteina.info
 
J

John Machin

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
 
P

Peter Otten

Alfons said:
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?

http://mail.python.org/pipermail/tutor/1999-March/000083.html

Peter
 
P

Paul McGuire

Alfons said:
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?

http://mail.python.org/pipermail/tutor/1999-March/000083.html

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):
http://pyparsing.wikispaces.com/file/view/chemicalFormulas.py

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

caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
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("C6H5OH")

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top