简体   繁体   中英

How to remove a substring conditionally from a list of strings?

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. ']

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. '] [' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ', ' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ']

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. ']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM