Reputation: 289
I have the set
set1={'*klj?', 'bl:VOLTe?', 'abkjld:Sure:STe?', 'JKLJS?', 'TRered[:AMide]?', 'DKJ[:dkja]?'}
I want to have the set look like
set1={'*klj?', 'bl:VOLTe?', 'abkjld:Sure:STe?', 'JKLJS?', 'TRered?','DKJ?'}
where I want to get rid of the [:AMide]
and [:dkja]
inside the set.
I was trying to use regex
What I have so far is
set2={}
for element in set:
x=re.sub("([\(\[]).*?([\)\]])", "", str(element))
set2.add(x)
This gets rid of the [] and what is inside but doesn't properly recreate the set, ie set2.add(x)
doesn't work
Upvotes: 0
Views: 75
Reputation: 2743
Here is another option
res = {re.sub('(:AMide)|(:dkja)', '', s) for s in set1}
{re.sub(']|\[', '', t) for t in res}
The output is:
>>>> {'*klj?', 'DKJ?', 'JKLJS?', 'TRered?', 'abkjld:Sure:STe?', 'bl:VOLTe?'}
Upvotes: 0
Reputation: 71461
You can try this:
import re
set1={'*klj?', 'bl:VOLTe?', 'abkjld:Sure:STe?', 'JKLJS?', 'TRered[:AMide]?', 'DKJ[:dkja]?'}
new_set = {re.sub('\[\:[a-zA-Z]+\]', '', i) for i in set1}
Output:
{'*klj?', 'abkjld:Sure:STe?', 'DKJ?', 'JKLJS?', 'TRered?', 'bl:VOLTe?'}
Upvotes: 0
Reputation: 107337
You don't need such a complicated regex for this task. Just use two replaces with a set-comprehension:
In [10]: {i.replace('[:AMide]', '').replace('[:dkja]', '') for i in set1}
Out[10]: {'*klj?', 'DKJ?', 'JKLJS?', 'TRered?', 'abkjld:Sure:STe?', 'bl:VOLTe?'}
After all, if you want to remove everything between brackets I think you could simply use a negated character class as following:
In [11]: import re
In [12]: {re.sub(r'\[[^]]+\]', r'', i) for i in set1}
Out[12]: {'*klj?', 'DKJ?', 'JKLJS?', 'TRered?', 'abkjld:Sure:STe?', 'bl:VOLTe?'}
Upvotes: 1
Reputation: 114440
Strings are immutable. You can not replace a string in-place. The proper way to modify your set is either to remove the offending elements and put in the correct versions, or to create an entirely new set. The latter approach is a one-liner:
set1 = set(re.sub("([\(\[]).*?([\)\]])", "\g<1>\g<2>", str(element)) for element in set1)
Upvotes: 1