Whether each element in the list is a child of other elements in the list

know a list, ask whether each element in this list is a child of another element, and if so, delete this element.

list = ["GH", "ACDB", "AB", "ABCFE", "ABDCFE", "ACFE", "ABCD", "ABD", "ACBD", "ACD", "FCBA", "FCDBA", "FCA", "FC", "BACF", "BCF", "BDCF", "BACFE", "BCFE", "BDCFE", "BACD", "BCD", "BD", "CBA", "CDBA", "CA", "CAB", "CDB", "CB", "EF", "EFC", "EFCABD", "EFCBD", "EFCD", "DBACF", "DBCF", "DCF", "DBAC", "DBC", "DC"]

as in the above code, the last element that should be left in list is

list=["ABDCFE","GH"]
Jul.19,2022


<br>

lst = ['GH', 'ACDB', 'AB', 'ABCFE', 'ABDCFE', 'ACFE', 'ABCD', 'ABD', 'ACBD', 'ACD', 'FCBA', 'FCDBA', 'FCA', 'FC', 'BACF', 'BCF', 'BDCF', 'BACFE', 'BCFE', 'BDCFE', 'BACD', 'BCD', 'BD', 'CBA', 'CDBA', 'CA', 'CAB', 'CDB', 'CB', 'EF', 'EFC', 'EFCABD', 'EFCBD', 'EFCD', 'DBACF', 'DBCF', 'DCF', 'DBAC', 'DBC', 'DC']

-sharp
lst1 = []
for i, x in enumerate(lst):
    index = i
    for y in lst:
        if x != y and len(set(x).intersection(set(y))) == len(x):
            index = -1
            break

    if index != -1:
        lst1.append(x)

print lst1

-sharp
lst1 = []
for i, x in enumerate(lst):
    index = i
    for y in lst:
        if x != y and x in y:
            index = -1
            break

    if index != -1:
        lst1.append(x)

print lst1
Menu