简体   繁体   English

我在 python 中有一个方法/函数,我试图在其中获取一个返回值,然后我可以将其用作另一个函数中的变量

[英]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.

相关问题 无法使创建的 python function 返回一个值,然后我可以在另一个计算中使用该值 - Can't make created python function return a value which I can then use in another calculation 如何在 Python 3 中获取一个函数以返回我可以在另一个函数中使用的值? - How can I get a function in Python 3 to return a value that I can use in another function? 如何确定要在python中定义的函数使用哪个变量 - How can I determine which variable to use for a defined function in python 我正在尝试在 python 中使用其 function 之外的变量 - I'm trying to use a variable outside of its function in python 如何从通过另一个 function (带参数)作为参数的 function 返回一个值? 我得到一个类型错误 - How to return a value from a function which was passed another function (with arguments) as an argument? I get a TypeError 为了进行这种转换,我可以在 Pandas dataframe 中使用哪个 Function? - Which Function can I use in Pandas dataframe in order to have this transformation? 如何在Python中调用带有存储为字符串的变量的函数 - How in Python can I call a function with a variable which is store as a string 我有一个带有函数名称的字符串变量,如何在Python中调用该函数? - i have a string variable which comes with function names, how do I call the function in Python? 我可以有条件地改变我正在呼叫的功能吗? - Can I conditionally change which function I'm calling? 我正在尝试显示使用输入 function 获得的字符串的索引值 - I am trying to display the index value of strings which I have obtained using the input function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM