我在 python 中编写了一个功能代码,但我不明白为什么它返回 None 而不是代码清楚生成的正确值。
此脚本的目的是从 CSV 中获取 line.split(',') 并将无意中从 '{value1,value2,value3}' 拆分的任何值重新组合为 'value1,value2 ... valueN'。
def reassemble(list_name):
def inner_iter(head, tail):
if head[0][0] == '{':
new_head = [head[0] + ',' + tail[0]]
if tail[0][-1] == '}':
return [new_head[0][1:-1]], tail[1:]
else:
inner_iter(new_head, tail[1:])
def outer_iter(corrected_list, head, tail):
if tail == []:
print corrected_list + head
return corrected_list + head
else:
if head[0][0] == '{':
head, tail = inner_iter(head, tail)
outer_iter(corrected_list + head, [tail[0]], tail[1:])
else:
outer_iter(corrected_list + head, [tail[0]], tail[1:])
return outer_iter([], [list_name[0]], list_name[1:])
下面是一个测试:
x = ['x','y', '{a', 'b}', 'c']
print reassemble(x)
这是奇怪的结果:
['x', 'y', 'a,b', 'c'] #from the print inside "outer_iter"
None #from the print reassemble(x)
注意:我想保持代码功能作为练习。