python函数返回字符串列表的问题?

时间:2015-01-02 04:11:46

标签: python list function python-2.7 return

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

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']

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

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

print fancy_function(a_directory,a_regex)

它返回我想要的5个预处理文件:

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

为什么会发生这种情况?如何获取列表中的5个文件?我想将它们保存在一个列表中以便进行其他处理但现在对于主列表中的每个列表,如下所示:

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

1 个答案:

答案 0 :(得分:3)

你在for循环中有一个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']]