Python 以新行打印列表中元素的递归方法

Python 以新行打印列表中元素的递归方法,python,recursion,Python,Recursion,到目前为止,我所拥有的: def print_nested_list(input): """Prints out every single string in input, one per line.""" if type(input) is list for item in input 感谢您的回复-我得到了这个结果,而不是仅仅使用for循环,我应该将它与调用print_nested_list函数结合使用 因此,为了完成测试用例: print_neste

到目前为止,我所拥有的:

def print_nested_list(input):
    """Prints out every single string in input, one per line."""
    if type(input) is list
           for item in input
感谢您的回复-我得到了这个结果,而不是仅仅使用for循环,我应该将它与调用print_nested_list函数结合使用

因此,为了完成测试用例:

print_nested_list(['cs1110'])
print_nested_list(['this', ['is', 'a'], 'list', ['list', 'list' ]])
print_nested_list([[['cs1110', 'opython'], 'nested'], 'recursion', 'test'])
为了澄清,最后一个测试用例应该如下所示:

cs1110
opython
nested
recursion
test

我想你应该这样做:

def print_nested_list(input_list):
    if type(input_list) is list:
        for item in input_list:
            print_nested_list(item)
    else:
        print input_list

mylist = [[['cs1110', 'opython'], 'nested'], 'recursion', 'test']
print_nested_list(mylist)
这种情况下的输出为:

cs1110
opython
nested
recursion
test

您当前的函数不是递归的,并且不检查任何东西是否是列表。你当然可以在这方面多做点努力。可能会重复阅读关于递归的文章。。我想说的是,我所需要做的不是打印项循环中的for项,而是调用我在该循环中再次定义的函数以使其递归?嗯,您编辑了代码,所以现在它缺少两件事:1-函数本身的递归调用,2-停止递归的条件。我建议您阅读更多关于递归的内容,以了解整个情况。例如: