On 13 Jan 2005, [EMAIL PROTECTED] wrote:
> 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
> Name=223473_at
> Cell1=307 87 N control 223473_at
> Cell2=307 88 N control 223473_at
> Cell3=367 84 N control 223473_at
>
> My Script:
>>>> name1 = '[N][a][m][e][=]'
>>>> for i in range(len(probe_pairs)):
> key = re.match(name1,probe_pairs[i])
> key
Here I see two things:
(a) The regexp is not what you possibly wanted: if you want a match for
a simple string then use just that string
(b) The common iterate idiom in Python is:
for elem in lst: ....
range() is not necesssary.
[...]
> Here it prints a bunch of reg.match objects. However
> when I say group() it prints only one object why?
You don't say how you write group().
> Alternatively:
>>>> for i in range(len(probe_pairs)):
> key = re.match(name1,probe_pairs[i])
> key.group()
>
>
> 'Name='
This is clear. The group is always the same: �Name=�
> 1. My aim:
> To remove those Name=**** lines from my probe_pairs
> list
This is easy. Since you always want to remove the samesimple string you
don't need a regexp; the string methgods will do a fine job here. A
slice will also be fine.
[...]
> After I get the reg.match object, I tried to remove
> that match object like this:
>>>> for i in range(len(probe_pairs)):
> key = re.match(name1,probe_pairs[i])
> del key
> print probe_pairs[i]
[...]
> Result shows that that Name** line has not been
> deleted.
You don't alter the original list so how could it be changed.
> Is the way I am doing a good one. Could you please
> suggest a good simple method.
A very simple one:
[entry.startswith('Name=') and entry[5:] or entry for entry in probe_pairs]
or:
map(lambda entry: entry.startswith('Name=) and entry[5:] or entry, probe_pairs)
or with Python >= 2.4 as generator expression:
(entry.startswith('Name=) and entry[5:] or entry for entry in probe_pairs)
The last one is nice; you could write the generator object to a file with:
outfile = file('probe_pairs_without_Name', 'w')
outfile.writelines(entry.startswith('Name=') and entry[5:] or entry for entry
in probe_pairs)
probe_pairs could also be a file object. That allows to work with huge
files without consuming too much memory.
inf = file('probe_pairs')
out = file('probe_pairs_without_Name', 'w')
out.writelines(entry.startswith('Name=') and entry[5:] or entry for entry in
inf)
inf.close()
out.close()
Karl
--
Please do *not* send copies of replies to me.
I read the list
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor