【发布时间】:2014-10-14 02:46:05
【问题描述】:
我写了以下函数:
def splitter(input, inc=0):
if inc == len(regex):
print "return point reached"
return input
print inc, input
output = []
if type(input) is list:
for i in input:
o = re.split(regex[inc],i)
if not o[0] == i:
for x in o:
output.append(x)
else:
output.append(i)
else:
o = re.split(regex[inc],input)
if not o[0] == input:
for x in o:
output.append(x)
else:
output.append(input)
inc+=1
splitter(output, inc)
正则表达式定义为:
regex = [' & ',' / ',' \+ ',' and ',' und ',' ft\. ',' feat\. ']
输入是一些类似的字符串
"Benga und Welsh ft. Warrior Queen and Skream feat. Benny Ill"
目标是在正则表达式中的条目指定的每个点处拆分字符串。
print inc, input 行打印的输出符合预期(它提供['Benga', 'Welsh', 'Warrior Queen', 'Skream feat. Benny Ill'] 作为输入),甚至声明print "return point reached" 的行正在执行,但函数似乎根本没有返回任何内容。
知道我从 django manage.py-shell 调用函数重要吗?
感谢任何帮助的想法! r
【问题讨论】:
-
请修正格式/缩进。否则,很难看出函数定义在哪里结束。
-
你在最后一行忘记
return splitter(output,inc) -
为什么不使用调试器?
-
您的缩进需要一些工作,因为现在发布的内容甚至无效。您的函数返回任何内容的唯一位置是
if inc == length(gex)。我怀疑你打算return output或函数末尾的东西,但我不确定你的最后两行是否打算成为函数的一部分,还是在它之外......
标签: python list function recursion return