Home > Software design >  How to remove a substring conditionally from a list of strings?
How to remove a substring conditionally from a list of strings?

Time:05-20

I have the following lists of strings:

my_list1 = [' If the display is not working ', "<xxx - tutorial section>", "tutorial section", ' display message appears. ']

my_list2 = [' If the display is not working ', "tutorial section", "<xxx - tutorial section>", ' display message appears. ']

How can I identify a string that is equal to a substring right after or before - surrounded by < and > symbols. In my example, this is tutorial section. I want to remove this string to get the following results:

my_list1_ed = [' If the display is not working ', "<xxx - tutorial section>", ' display message appears. ']

my_list2_ed = [' If the display is not working ', "<xxx - tutorial section>", ' display message appears. ']

If a list does not contain this kind of duplicates, then nothing should be done. How can I implement this logic?

My attempt:

final_list = []
lists = my_list1   my_list2
for i, v in enumerate(lists):
   if (v not in lists[i-1]) or (v not in lists[i 1]):
       final_list.append(v)

The result that I get (duplicates still are there):

[' If the display is not working ',
 '<xxx - tutorial section>',
 'tutorial section',
 ' display message appears. ',
 ' If the display is not working ',
 'tutorial section',
 '<xxx - tutorial section>',
 ' display message appears. ']

CodePudding user response:

Changed the code a bit, see if helps.

final_list = []
lists = my_list1   my_list2

for i, v in enumerate(lists):
    if i == 0:
        if v not in lists[i 1]:
            final_list.append(v)
    elif i == len(lists) - 1:
        if v not in lists[i-1]:
            final_list.append(v)
    else:
        if (v not in lists[i-1]) and (v not in lists[i 1]):
            final_list.append(v)

print(final_list)

output: [' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ', ' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ']

CodePudding user response:

here's how I'll do it.

my_list1 = [' If the display is not working ', "<xxx - tutorial section>", "tutorial section", ' display message appears. ']

my_list2 = [' If the display is not working ', "tutorial section", "<xxx - tutorial section>", ' display message appears. ']

all_l = []
new_list1 = []
new_list2 = []
matches = ['-','<','>']
for i in range(len(my_list1)):
    if any(x in my_list1[i] for x in matches):
       nl1 = [r for r in my_list1 if not r in my_list1[i] or r == my_list1[i]]
       new_list1 = nl1
    if any(x in my_list2[i] for x in matches):
       nl2 = [r for r in my_list2 if not r in my_list2[i] or r == my_list2[i]]
       new_list2 = nl2
  
print(new_list1)
print(new_list2)

Output:

[' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ']
[' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ']
  • Related