如何從 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?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章