[Tutor] How to substitute an element of a list as a pattern for re.compile()

kumar s ps_python at yahoo.com
Thu Dec 30 02:01:23 CET 2004

Hi Group:

I have Question: 
How can I substitute an object as a pattern in making
a pattern. 

>>> x = 30
>>> pattern = re.compile(x)

My situation:

I have a list of numbers that I have to match in
another list and write them to a new file:

List 1: range_cors 
>>> range_cors[1:5]
['161:378', '334:3', '334:4', '65:436']

List 2: seq
>>> seq[0:2]
Interrogation_Position=3330; Antisense;',

A slow method:
>>> sequences = []
>>> for elem1 in range_cors:
	for index,elem2 in enumerate(seq):
		if elem1 in elem2:

This process is very slow and it is taking a lot of
time. I am not happy.

A faster method (probably):

>>> for i in range(len(range_cors)):
	for index,m in enumerate(seq):
		pat = re.compile(i)
		if re.search(pat,seq[m]):

I am getting errors, because I am trying to create an
element as a pattern in re.compile(). 


1. Is it possible to do this. If so, how can I do

Can any one help correcting my piece of code and
suggesting where I went wrong. 

Thank you in advance. 


Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

More information about the Tutor mailing list