简体   繁体   English

python在函数之间传递列表

[英]python passing list between functions

I am struggling to pass a list ( holder , which contains [0,0] ) to another function. 我正在努力将列表( holder ,包含[0,0] )传递给另一个函数。 I want check_neighbours to pick up the list and then do some further processing. 我想check_neighbours拿起列表,然后做一些进一步的处理。 I think I am passing it correctly in main() but I am not sure what I must do to get the list to simply display its contents when called upon inside check_neighbours . 我想我在main()正确传递它但是我不确定我必须做什么才能让列表在check_neighbours内部调用时显示其内容。 I have tried a = holder but I received the error: 我试过一个=持有人,但我收到了错误:

global name "holder" is not defined 全局名称“持有者”未定义

I believe I must put holder = #something here but I cannot figure out what. 我相信我必须把holder = #something这里,但我无法弄清楚是什么。

 def create_matrix(file):
    with open('network.txt') as f:
        Alist = []
        for line in f:
            part = []
            for x in line.split(','):
                part.append(int(x))
            Alist.append(part)
    return Alist

def start_node(Alist):
        i=0
        j=0
        #point node to pos [0][0] of Alist
        node = Alist[i][j]
        #create a list to hold co-ordinates
        holder = []
        holder.append(i)
        holder.append(j)
        print holder

        return node, holder

#test neighbours to see if they can be used
def check_neighbours(node, Alist):
        holder = #something 



#code begins here
def main():
        file = ("F:/media/KINGSTON/Networking/network.txt")
        Alist = create_matrix(file)
        node = start_node(Alist)
        holder = check_neighbours(node, Alist)
main()

At the end of start_node , you do this: start_node结束时,您执行以下操作:

return node, holder

… but when you call it, you do this: ...但是当你打电话时,你这样做:

node = start_node(Alist)

That means your local node variable ends up as a tuple of node, holder . 这意味着您的本地node变量最终成为node, holder的元组。 You don't want that. 你不希望这样。 You want this: 你要这个:

node, holder = start_node(Alist)

Meanwhile, you say "I think I am passing it correctly in the main() function", but you're not passing it at all in the main function: 同时,你说“我认为我在main()函数中正确传递它”,但你在main函数中根本没有传递它:

holder = check_neighbours(node, Alist)

There's no way for check_neighbours to get holder here, because you're not giving it holder here. check_neighbours无法在这里获得holder ,因为你没有在这里给予holder

Plus, when you defined check_neighbours , you did this: 另外,当你定义check_neighbours ,你这样做了:

def check_neighbours(node, Alist):
    holder = #something 

This doesn't take a holder parameter, it defines a new local variable named holder . 这不带一个holder参数,它定义了一个名为holder的新局部变量。 So, change those two lines to: 所以,将这两行改为:

def check_neighbours(node, Alist, holder):

And call it with: 并称之为:

holder = check_neighbours(node, Alist, holder)

Also, note that you're assigning the result of check_neighbors back to the holder variable, replacing whatever you passed in. Often, that's a perfectly reasonable thing to do (think my_name = my_name.replace('Joseph', 'Joe') ), but make sure it really is what you want (and, of course, make sure that check_neighbors ends by doing a return holder , or returning some other appropriate value). 另外,请注意,您将check_neighbors的结果分配回holder变量,替换您传入的任何内容。通常,这是完全合理的事情(想想my_name = my_name.replace('Joseph', 'Joe') ) ,但要确保它确实是你想要的(当然,确保check_neighbors通过做一个return holder ,或返回一些其他适当的值来结束)。

Finally, as I said in your other question: If you keep using the same name for your parameters and the values you pass in, you're going to keep confusing yourself like this. 最后,正如我在你的另一个问题中所说:如果你继续使用相同的名称作为你的参数和你传入的值,那么你将会像这样迷惑自己。 If you used different names, it would be much easier for you to see what you're missing. 如果您使用不同的名称,那么您将更容易看到您所缺少的内容。 Even something as simple as a prefix on each one: 甚至像每个人的前缀一样简单:

def main():
    file = ("F:/media/KINGSTON/Networking/network.txt")
    my_list = create_matrix(file)
    my_node, my_holder = start_node(my_list)
    my_new_holder = check_neighbours(my_node, my_list, my_holder)

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

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