求助大佬
def cleanup(solutionstr):solutionstr.replace(" ", "")
solutionstr.split(">", -1)
print(solutionstr)
我想定义一个函数 除去字符串 “| MCGW > G| MCW” 中的空格并将其从“>” 处分开输出为一个列表为什么这个定义的函数输出的结果与输入的一样?
def cleanup(solutionstr):
solutionstr = solutionstr.replace(" ", "")
result = solutionstr.split(">", -1)
print(result)
strTest = '| MCGW > G| MCW'
cleanup(strTest) 我们把函数拆开,一行一行的去运行
函数过程并没有传递回solutionstr
>>> solutionstr= "| MCGW > G| MCW"
>>> solutionstr.replace(" ", "")
'|MCGW>G|MCW'
>>> solutionstr.split(">", -1)
['| MCGW ', ' G| MCW']
>>> print(solutionstr)
| MCGW > G| MCW
修改代码
def cleanup(solutionstr):
solutionstr = solutionstr.replace(" ", "")
solutionstr = solutionstr.split(">", -1)
print(solutionstr)
页:
[1]