如何从字符串列表中有条件地删除子字符串?
How to remove a substring conditionally from a list 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. ']
如何识别等于 -
之后或之前被 <
和 >
符号包围的子字符串的字符串。在我的示例中,这是 tutorial section
。我想删除此字符串以获得以下结果:
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. ']
如果列表不包含此类重复项,则不应执行任何操作。我该如何实现这个逻辑?
我的尝试:
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)
我得到的结果(仍然存在重复):
[' 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. ']
稍微修改一下代码,看看是否有帮助。
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)
输出:[' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ', ' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ']
以下是我的操作方法。
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)
输出:
[' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ']
[' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ']
我有以下字符串列表:
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. ']
如何识别等于 -
之后或之前被 <
和 >
符号包围的子字符串的字符串。在我的示例中,这是 tutorial section
。我想删除此字符串以获得以下结果:
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. ']
如果列表不包含此类重复项,则不应执行任何操作。我该如何实现这个逻辑?
我的尝试:
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)
我得到的结果(仍然存在重复):
[' 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. ']
稍微修改一下代码,看看是否有帮助。
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)
输出:[' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ', ' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ']
以下是我的操作方法。
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)
输出:
[' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ']
[' If the display is not working ', '<xxx - tutorial section>', ' display message appears. ']