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

Jacob S. keridee at jayco.net
Sat Jan 1 22:16:31 CET 2005


Nobody explicitly mentioned that you're trying to make a pattern from an
integer and not a string, which, I believe is required. Also, Rich needs to
make the % formatting "%d"%x   instead of  "%s"%x  because he showed that x
is an integer not a string.

There's my two bits.
Jacob Schmidt


> 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]
> ['>probe:HG-U133A_2:1007_s_at:416:177;
> Interrogation_Position=3330; Antisense;',
> 'CACCCAGCTGGTCCTGTGGATGGGA']
>
>
> A slow method:
> >>> sequences = []
> >>> for elem1 in range_cors:
> for index,elem2 in enumerate(seq):
> if elem1 in elem2:
> sequences.append(elem2)
> sequences.append(seq[index+1])
>
> 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]):
> p.append(seq[m])
> p.append(seq[index+1])
>
>
> I am getting errors, because I am trying to create an
> element as a pattern in re.compile().
>
>
> Questions:
>
> 1. Is it possible to do this. If so, how can I do
> this.
>
> Can any one help correcting my piece of code and
> suggesting where I went wrong.
>
> Thank you in advance.
>
>
> -K
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>



More information about the Tutor mailing list