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

前端 未结 8 2126
清酒与你
清酒与你 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:02

    Here is a one-liner that does what you want:

    filter(lambda x: [x for i in string_list if x in i and x != i] == [], string_list)
    

    Example:

    >>> string_list = ['rest', 'resting', 'look', 'looked', 'it', 'spit']
    >>> filter(lambda x: [x for i in string_list if x in i and x != i] == [], string_list)
    ['resting', 'looked', 'spit']
    

提交回复
热议问题