簡體   English   中英

如何從 Python 中的另一個 function 調用列表?

[英]How do I call a list from another function in Python?

我在 function 中有一個列表,我已將值附加到其中,現在我想知道如何在另一個 function 中調用該列表。

我不知道我是否完全理解您,但您可以return該列表並將其用於另一個 function。

def function():
    x=[]
    for i in range(5):
        x.append(i)
return x

def print_function():
    x=function()
    print(x)
return

您可以聲明一個新的全局變量以將您的列表指向 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM