1

我在 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)

注意:我想保持代码功能作为练习。

4

2 回答 2

4

你忘了returnelse子句中,在outer_iter

在 python 中,不返回特定值的函数返回None.

[编辑]

完整来源:

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:
                return inner_iter(new_head, tail[1:])
                #before the change, control reached here upon return
        #control might still reach here, in case 'if' condition was not met
        #implicitly "return None"

    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)
                return outer_iter(corrected_list + head, [tail[0]], tail[1:])
                #before the change, control reached here upon return
            else:
                return outer_iter(corrected_list + head, [tail[0]], tail[1:])
                #before the change, control reached here upon return
        #before the change, control reached here upon return
        #implicitly "return None"

    return outer_iter([], [list_name[0]], list_name[1:])

测试:

x = ['x','y', '{a', 'b}', 'c']
print reassemble(x)

输出:

['x', 'y', 'a,b', 'c']
['x', 'y', 'a,b', 'c']
于 2013-04-25T12:11:52.070 回答
1
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:
                return 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)
                return outer_iter(corrected_list + head, [tail[0]], tail[1:])

            else:
                return 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)

查看此 Bunk 中的运行代码http://codebunk.com/bunk#-It06-ImaZDpSrCrQSmM

于 2013-04-25T12:13:10.850 回答