[英]How do I call a list from another function in Python?
I have a list in a function that I have appended values into, now I am trying to wonder how I can call that list in another function.我在 function 中有一个列表,我已将值附加到其中,现在我想知道如何在另一个 function 中调用该列表。
I don't know if I completely understand you but you can return
that list and use it into another function.我不知道我是否完全理解您,但您可以
return
该列表并将其用于另一个 function。
def function():
x=[]
for i in range(5):
x.append(i)
return x
def print_function():
x=function()
print(x)
return
You can declare a new global variable to point your list inside the function.您可以声明一个新的全局变量以将您的列表指向 function 内。
pointedList = []
copiedList = []
def function():
x = []
x.append("Something")
pointedList = x #Changes made to pointedList will change values in x
copiedList = x.copy() #Changes made to copiedList will not reflect in x
'''
Rest of the program
'''
def newFunction():
'''
You can use pointedList and copiedList here
'''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.