[Tutor] Regular expression re.search() object . Please help

Alan Gauld alan.gauld at freenet.co.uk
Fri Jan 14 00:27:56 CET 2005


> My list looks like this: List name = probe_pairs
> Name=AFFX-BioB-5_at
> Cell1=96 369 N control AFFX-BioB-5_at
> Cell2=96 370 N control AFFX-BioB-5_at
> Cell3=441 3 N control AFFX-BioB-5_at
> Cell4=441 4 N control AFFX-BioB-5_at
> ...
> My Script:
> >>> name1 = '[N][a][m][e][=]'

Why not just: 'Name=' - the result is the same.

> >>> for i in range(len(probe_pairs)):

andwhy not just
   for line in probe_pairs:
      key = re.match(name1,line)

Although I suspect it would be easier still to use

line.startswith('Name=')

especially combined with the fileinput module.
It is really quite good for line by line
matching/processing of files, and I assume this
data comes from a file originally?.

> key = re.match(name1,probe_pairs[i])
> key
> <_sre.SRE_Match object at 0x00E37A68>

One per line that matches.

> when I say group() it prints only one object why?

Because the group is the string you are looking for.
But I may be missing something since there is no
indentation in the post, its hard to tell whats
inside and whats outside the loop.

> 1. My aim:
> To remove those Name=**** lines from my probe_pairs
> list

> >>> for i in range(len(probe_pairs)):
> key = re.match(name1,probe_pairs[i])
> del key

That will remove the match object, not the line from
the list!

To filter the list I'd have thought you'd be better using
a list comprehension:

filtered = [line for line in probe_pairs if not
line.startswith('Name=')]

HTH,

Alan G.



More information about the Tutor mailing list