如何从 Python 中的一组字符串中删除特定的子字符串? - How to remove specific substrings from a set of strings in Python?

问题:

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.我有一组字符串的set1 ,并在所有的字符串set1有我不需要,想删除两个具体子。
Sample Input: set1={'Apple.good','Orange.good','Pear.bad','Pear.good','Banana.bad','Potato.bad'}样本输入: set1={'Apple.good','Orange.good','Pear.bad','Pear.good','Banana.bad','Potato.bad'}
So basically I want the .good and .bad substrings removed from all the strings.所以基本上我想要的.good.bad从所有的字符串删除子。
What I tried:我试过的:

for x in set1:
    x.replace('.good','')
    x.replace('.bad','')

But this doesn't seem to work at all.但这似乎根本不起作用。 There is absolutely no change in the output and it is the same as the input.输出绝对没有变化,它与输入相同。 I tried using for x in list(set1) instead of the original one but that doesn't change anything.我尝试使用for x in list(set1)而不是原始的,但这不会改变任何东西。


解决方案:

参考一: https://stackoom.com/question/2WoJv
参考二: How to remove specific substrings from a set of strings in Python?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章