从列表中删除特定子字符串在其他元素中重复的元素

2024-04-27 01:16:22 发布

您现在位置:Python中文网/ 问答频道 /正文

如何查找列表元素的一个或多个特定子字符串是否重复。与列表中的其他元素相同,然后只保留第一个包含这些子字符串的元素并删除其他元素(包含重复项的元素),从而使列表唯一。你知道吗

示例:

SUBSTRINGS=['banana','chocolate']
MYLIST=['1 banana cake','2 banana cake','3 cherry cake','4 chocolate cake','5 chocolate cake','6 banana cake','7 pineapple cake']

在这种情况下,重复的子串是bananachocolate案子。之后列表的处理过程应为:

MYLIST=['1 banana cake','3 cherry cake','4 chocolate cake','7 pineapple cake']

Tags: 字符串元素示例列表情况bananacherrycake
2条回答

在这里,我们通过在原始的MYLIST上迭代来构造一个列表new_list。我们使用all_substrings集跟踪哪些子字符串(来自SUBSTRINGS)已经被使用。你知道吗

SUBSTRINGS = {'banana', 'chocolate'}
MYLIST = ['1 banana cake', '2 banana cake', '3 cherry cake', '4 chocolate cake', '5 chocolate cake', '6 banana cake', '7 pineapple cake']

new_list = []
all_substrings = set()
for el in MYLIST:
    # All substrings of this element
    substrings = set(el.split())
    # Add this element if it does not have any substrings in common
    # with the all_substrings set.
    if not any(substring in all_substrings for substring in substrings):
        new_list.append(el)
    # Add current substrings which are also present
    # in SUBSTRINGS to all_substrings.
    all_substrings |= (substrings & SUBSTRINGS)
print(new_list)

这里有一个比jmd_dk发布的答案更简单的答案。据我所知,两者都很好用。你知道吗

SUBSTRINGS = {'banana', 'chocolate'}
MYLIST = ['1 banana cake', '2 banana cake', '3 cherry cake', '4 chocolate cake', '5 chocolate cake', '6 banana cake', '7 pineapple cake']
m = []

for substr in MYLIST:
    if any(el in substr for el in SUBSTRINGS):
        if not any(substr.split()[1] in n for n in m):
            m.append(substr)
    else:
        m.append(substr)

print(m)

相关问题 更多 >