How to remove specific substrings from a set of strings in Python?

后端 未结 10 1936
自闭症患者
自闭症患者 2020-12-07 12:08

I have a set of strings set1, and all the strings in set1 have a two specific substrings which I don\'t need and want to remove.
Sample Input

10条回答
  •  盖世英雄少女心
    2020-12-07 12:26

    If list

    I was doing something for a list which is a set of strings and you want to remove all lines that have a certain substring you can do this

    import re
    def RemoveInList(sub,LinSplitUnOr):
        indices = [i for i, x in enumerate(LinSplitUnOr) if re.search(sub, x)]
        A = [i for j, i in enumerate(LinSplitUnOr) if j not in indices]
        return A
    

    where sub is a patter that you do not wish to have in a list of lines LinSplitUnOr

    for example

    A=['Apple.good','Orange.good','Pear.bad','Pear.good','Banana.bad','Potato.bad']
    sub = 'good'
    A=RemoveInList(sub,A)
    

    Then A will be

提交回复
热议问题