473,320 Members | 1,933 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,320 software developers and data experts.

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
al***********@upf.edu - http://cgl.imim.es

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

Oct 20 '08 #1
3 949
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
Oct 20 '08 #2
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?
http://mail.python.org/pipermail/tut...ch/000083.html

Peter
Oct 20 '08 #3
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?

http://mail.python.org/pipermail/tut...ch/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...calFormulas.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
Oct 20 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Benedikt Wismans | last post by:
dear group! <script language="javascript"> var a = 60381.11; var b = 1437261.58; var c = a + b; alert (c); -> 1497642.6900000002 var a = 60381.11; var b = 1437261.50;
4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
17
by: Steve Jorgensen | last post by:
If you've ever employed custom error numbers and messages in you programs, you've probably ended up with code similar to what I've ended up with in the past something like... <code> public...
19
by: youpak2000 | last post by:
Are MAGIC numbers always bad? Using magic numbers (constant numbers) in programs are generally considered a bad programming practice, and it's recommended that to define constants in single,...
11
by: Ed Jay | last post by:
I'm having difficulties arithmetically manipulating form element values. I've entered data into the form, and I fetch them using a js, as: p7Left = Number(document.form1.elements.value);...
23
by: ultimatewarrior | last post by:
Hi all, first of all I beg your pardon if this question has been asked before, but I was unable to find anything in the past posts. I have written a piece of code that was supposed to be quite...
1
by: Lie Ryan | last post by:
On Mon, 20 Oct 2008 13:16:48 +0200, Alfons Nonell-Canals wrote: That's the job of regular expression: 'import re' numbered_atom = re.compile('?+') if numbered_atom.match('C10'): # this is a...
7
by: NDayave | last post by:
How do, I want to be able to make a certain number of variables depending on the number of data items i have to be used. For example, i would need 3 variables defined when i have 3 numbers and...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.