[英]I have a method / function in python in which i'm trying to get a return value which i can then use as a variable in another function
So i'm trying to learn Python and at the same time write some code to help myself out with some mundane tasks.所以我正在尝试学习 Python,同时编写一些代码来帮助自己完成一些平凡的任务。 so i have a function which works just fine if i use the print statement to display just what i need, but when i replace the " print " statement for a " return " i only get the first element in the list, i know its because "return" causes the function to exit, but i have no idea how i can get this to return the rest of the elements.
所以我有一个函数,如果我使用打印语句来显示我需要的内容,它就可以正常工作,但是当我将“打印”语句替换为“返回”时,我只得到列表中的第一个元素,我知道它是因为“返回”导致函数退出,但我不知道如何让它返回其余元素。 i have tried lots of things and been now reading for hours :( and still getting no further. objective: To read all the "*.common" files in a directory and then compare them with a text file containing some names, and if they match return the name of the *.common file so i can use it in another function as a variable. code i have thus far
我已经尝试了很多东西,现在已经阅读了几个小时:(但仍然没有进一步。目标:读取目录中的所有“*.common”文件,然后将它们与包含一些名称的文本文件进行比较,如果它们match 返回 *.common 文件的名称,因此我可以在另一个函数中将它用作变量。到目前为止我拥有的代码
# This method gets the list of common files....
def get_common_files():
path = (config_home)
files = []
for r, d, f in os.walk(path):
for file in f:
if '.common' in file:
files.append(file)
for f in files:
return files
new_file = get_common_files()
def build_default_manifest():
for line in fileinput.FileInput(new_manifest):
for name in new_file:
if line.strip() in name:
print(name) # works as it should and displays all that i need
return name # only shows me the first element
good_name = build_default_manifest()
print(good_name)
i hope i've provided all that i need to but if not please feel free to comeback to me...我希望我已经提供了我需要的一切,但如果没有,请随时回来找我...
When you do this:当你这样做时:
for f in files:
return files
This is not quite right.这不太对。 There are 2 problems here.
这里有2个问题。 First, what this is doing is just returning
files
-- the whole thing, one time.首先,它所做的只是返回
files
——整个过程,一次。 Putting a loop body around the return
doesn't really make sense.在
return
周围放置一个循环体并没有真正意义。 Likely you meant to type return f
, to iterate over the results.... but that leads to a second problem...可能你想输入
return f
,迭代结果......但这会导致第二个问题......
The return
statement will run the first time it is encountered, causing the function to end, and that's it. return
语句将在第一次遇到时运行,导致函数结束,仅此而已。 So just changing what is returned to return f
still doesn't quite work: it will return only the first value in files
.因此,仅更改返回的内容
return f
仍然不起作用:它只会返回files
的第一个值。
This is actually really close to a slightly more advanced pattern called a generator.这实际上非常接近于称为生成器的稍微更高级的模式。 If you replace the above with this:
如果你用这个替换上面的:
for f in files:
yield f
then your function should work.那么你的功能应该可以工作。 Explaining why it works is not quite as easy, but this question has some good answers that should help.
解释它为什么起作用并不那么容易,但是这个问题有一些很好的答案应该会有所帮助。
Just out of curiosity, did you write this from scratch yourself, or did you follow an example from somewhere?出于好奇,您是自己从头开始编写的,还是从某个地方遵循示例? I'm just a bit surprised at how close you got to the generator pattern, apparently unintentionally, and assuming you were not aware of it before.
我只是有点惊讶您与生成器模式的接近程度,显然是无意的,并且假设您之前没有意识到它。
def build_default_manifest():
for line in fileinput.FileInput(new_manifest):
for name in new_file:
if line.strip() in name:
yield name
good_name = build_default_manifest()
for name in good_name:
print(name)
like Z4-tier said, sounds like you need a generator.就像 Z4-tier 说的,听起来你需要一个发电机。
you can use each element in good_name for a function call like normal.您可以像平常一样使用 good_name 中的每个元素进行函数调用。
Perhaps I'm missing something here but isn't your for loop (for r, d...) already creating your list called 'files'?也许我在这里遗漏了一些东西,但您的 for 循环(for r、d...)不是已经创建了名为“文件”的列表吗? So, why not just change:
那么,为什么不直接改变:
for f in files:
return files
to到
return files
which should just return the entire list?哪个应该只返回整个列表?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.