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

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)

このバンクで実行中のコードを参照してください http://codebunk.com/bunk#-It06-ImaZDpSrCrQSmM

于 2013-04-25T12:13:10.850 に答える