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

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

    All you need is a bit of black magic!

    >>> a = ["cherry.bad","pear.good", "apple.good"]
    >>> a = list(map(lambda x: x.replace('.good','').replace('.bad',''),a))
    >>> a
    ['cherry', 'pear', 'apple']
    

提交回复
热议问题