問題:
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?