我的目录中有一组文件。因此,我创建了一个函数,对目录中的所有文件进行一些处理:
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
打印 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']]
最佳答案
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']]
关于python - 返回字符串列表的 python 函数有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27737279/