简体   繁体   English

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

[英]Problems with a python function that return a list of strings?

I have a set of files in a directory. 我在目录中有一组文件。 So I created a function that apply some processing to all the files in the directory: 所以我创建了一个函数,对目录中的所有文件应用了一些处理:

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

Then I do the following: 然后我做以下事情:

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

It just return one list and the directory actually have 5 files, then when I do the following: 它只返回一个列表,目录实际上有5个文件,然后当我执行以下操作时:

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

print fancy_function(a_directory, a_regex) print fancy_function(a_directory,a_regex)

It returns the 5 preprocessed files that I want like this: 它返回我想要的5个预处理文件:

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

Why is this happening and how can I obtain the 5 files in a list?. 为什么会发生这种情况,如何获取列表中的5个文件? I would like to save them In one list in order to make a nother processing but now for each list in the main list, something like this: 我想将它们保存在一个列表中以便进行其他处理但现在对于主列表中的每个列表,如下所示:

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

You have a return statement inside a for loop, which is a common gotcha. 你在for循环中有一个return语句,这是一个常见的问题。 The function ends immediately, returning a single element, instead of returning a list of all the processed elements. 该函数立即结束,返回单个元素,而不是返回所有已处理元素的列表。

You have two options. 你有两个选择。 First, you can explicitly define a list within your function, append intermediate results to that list, and return the list at the end. 首先,您可以在函数中显式定义列表,将中间结果附加到该列表,并在结尾返回列表。

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

Or fancier, you can turn your function into a 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

This generator can then be used thus: 然后可以使用此生成器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM