473,500 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1067
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5739
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
10720
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
7438
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
5138
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
1205
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
638
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...
3
953
by: Alfons Nonell-Canals | last post by:
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...
7
8160
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
9589
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
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.