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

后端 未结 10 1939
自闭症患者
自闭症患者 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:33

    You could do this:

    import re
    import string
    set1={'Apple.good','Orange.good','Pear.bad','Pear.good','Banana.bad','Potato.bad'}
    
    for x in set1:
        x.replace('.good',' ')
        x.replace('.bad',' ')
        x = re.sub('\.good$', '', x)
        x = re.sub('\.bad$', '', x)
        print(x)
    

提交回复
热议问题