def replace_chars(stringa, listb):
'''A function to return stringa by removing the list of characters from listb'''
#print(stringa)
#print(listb)
for x in stringa:
if x == listb:
stringa.replace(x,listb)
return stringa
if __name__ == "__main__":
import test
test.testEqual(replace_chars(" My!Name*Is#John*",["!", "*", "#", "*"]), " My Name Is John ")
基本上,我应该将列表中的字符从测试中的字符串中取出。我不确定为什么这不起作用任何帮助将不胜感激。我需要它作为这样的函数,所以我可以将其导入其他文件。
感谢您的时间
答案 0 :(得分:1)
尝试:
def replace_chars(stringa, listb):
'''A function to return stringa by removing the list of characters from listb'''
#print(stringa)
#print(listb)
for x in listb:
stringa = stringa.replace(x,' ')
return stringa
if __name__ == "__main__":
#import test
print(replace_chars(" My!Name*Is#John*",["!", "*", "#", "*"]))