469,306 Members | 2,471 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Re: Idenfity numbers in variables

On Mon, 20 Oct 2008 13:16:48 +0200, Alfons Nonell-Canals 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,...
That's the job of regular expression: 'import re'

numbered_atom = re.compile('[A-Z][a-z]?[0-9]+')
if numbered_atom.match('C10'):
# this is a numbered atom
if numbered_atom.match('C'):
# this WON'T match

read more about regular expression on the web (hint: python share the
same re syntax with many other languages, php, javascript, grep, etc)

Oct 20 '08 #1
1 971
Lie Ryan <li******@gmail.comwrote:
That's the job of regular expression: 'import re'

numbered_atom = re.compile('[A-Z][a-z]?[0-9]+')
if numbered_atom.match('C10'):
# this is a numbered atom
if numbered_atom.match('C'):
# this WON'T match

read more about regular expression on the web (hint: python share the
same re syntax with many other languages, php, javascript, grep, etc)
Or simply:

if atom[-1].isdigit():
# this is a numbered atom
else:
# this isn't
Oct 20 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Benedikt Wismans | last post: by
17 posts views Thread by Steve Jorgensen | last post: by
19 posts views Thread by youpak2000 | last post: by
11 posts views Thread by Ed Jay | last post: by
23 posts views Thread by ultimatewarrior | last post: by
3 posts views Thread by Alfons Nonell-Canals | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.