return 字符串列表的 python 函数有问题吗?

Problems with a python function that return a list of strings?

我在一个目录中有一组文件。所以我创建了一个函数,对目录中的所有文件应用一些处理:

def fancy_function(directory, regex):
    for set the path of the directory:
       with open the file names and walk over them:
           preprocessing1 = [....]
           preprocessing2 = [ remove some punctuation from preprocessing1]

           return preprocessing2

然后我执行以下操作:

list_of_lists = fancy_function(a_directory, a_regex)
print list_of_lists
>>>['processed string']

它只是return一个列表,目录实际上有5个文件,然后当我执行以下操作时:

def fancy_function(directory, regex):
    do preprocessing...
    preprocessing1 = [....]
    preprocessing2 = [ remove some punctuation from preprocessing1]
    print preprocessing2

打印fancy_function(a_directory, a_regex)

它return是我想要的 5 个预处理文件,如下所示:

['file one']
['file two']
['file three']
['file four']
['file five']

为什么会这样,我怎样才能获得列表中的 5 个文件?。我想将它们保存在一个列表中以便进行其他处理,但现在对于主列表中的每个列表,如下所示:

main_list =[['file one'], ['file two'], ['file three'], ['file four'], ['file five']]

您在 for 循环中有一个 return 语句,这是一个常见问题。该函数立即结束,returning 单个元素,而不是 returning 所有已处理元素的列表。

你有两个选择。 首先,您可以在函数中明确定义一个列表,将中间结果附加到该列表,然后 return 列表最后。

def fancy_function(directory, regex):
    preprocessed_list = []
    for set the path of the directory:
        with open the file names and walk over them:
            preprocessing1 = [....]
            preprocessing2 = [ remove some punctuation from preprocessing1]

            preprocessed_list.append(preprocessing2)
    return preprocessed_list

或者更高级,您可以将函数变成 generator

def fancy_function(directory, regex):
    preprocessed_list = []
    for set the path of the directory:
        with open the file names and walk over them:
            preprocessing1 = [....]
            preprocessing2 = [ remove some punctuation from preprocessing1]

            yield preprocessing2 # notice yield, not return

这个生成器可以这样使用:

>>> preprocessed = fancy_function(a_directory, a_regex)
>>> print list(preprocessed)
[['file one'], ['file two'], ['file three'], ['file four'], ['file five']]