Python - Remove any element from a list of strings that is a substring of another element

前端 未结 8 2075
清酒与你
清酒与你 2020-12-06 10:09

So starting with a list of strings, as below

string_list = [\'rest\', \'resting\', \'look\', \'looked\', \'it\', \'spit\']

I wan

相关标签:
8条回答
  • 2020-12-06 11:09

    Another one liner:

    [string for string in string_list if len(filter(lambda x: string in x,string_list)) == 1]
    

    should be fairly readable, just not that pythonic.

    0 讨论(0)
  • 2020-12-06 11:10

    Here's an un-optimal way, only use if the lists are small:

    for str1 in string_list:
        for str2 in string_list:
            if str1 in str2:
                string_list.remove(str1)
    
    0 讨论(0)
提交回复
热议问题