簡體   English   中英

對 python 中的返回如何工作感到困惑

[英]Confused about how the return works in python

程序運行並且 function 工作,但我無法在 output 中看到我的 docCountryList。 有人能告訴我為什么嗎?

我有這個代碼

def ViewByCountry(docID,user_selection):
    docCountryList=[]
    for x in jfile:
        if x.get('subject_doc_id') == docID:
            docCountryList.append(x['visitor_country'])
    if user_selection == '2a':
        x = []
        y = []
        #Insert countries and number of occurences in two seperate lists
        for k,v in Counter(docCountryList).items():
            x.append(k)
            y.append(v)
        plt.title('Countries of Viewers')
        plt.bar(range(len(y)), y, align='center')
        plt.xticks(range(len(y)), x, size='small')
        plt.show()
        return docCountryList

在我的主要

from program import ViewByCountry

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    docID = input("Enter required document ID: ")
    user_selection = input("Enter selection")
    ViewByCountry(docID,user_selection)

你永遠不會打印出docCountryList的值,所以試試這個:

print(ViewByCountry(docID,user_selection))

這將打印出該值。

你也可以這樣做:

lst = ViewByCountry(docID,user_selection)
print(lst)

在您的主要內容中,您可以更改為myView = ViewByCountry(docID,user_selection) ,然后添加print(myView) 這會將您的 function 創建的列表保存到一個變量中,以便稍后打印或使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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