【问题标题】:Confused about how the return works in python [closed]对返回如何在 python 中工作感到困惑 [关闭]
【发布时间】:2021-11-29 16:03:13
【问题描述】:

程序运行并且函数工作,但我无法在输出中看到我的 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)

【问题讨论】:

  • print(ViewByCountry(docID,user_selection))
  • 您是否打算仅在user_selection == '2a' 时返回国家/地区列表?
  • 欢迎来到 Stack Overflow。您的代码缺少导入,并且不清楚这些对象的一半是多少。请编辑问题以包含"Minimal, Reproducible, Example."

标签: python histogram


【解决方案1】:

你永远不会打印出docCountryList 的值,所以试试这个:

print(ViewByCountry(docID,user_selection))

这将打印出值。

你也可以这样做:

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

【讨论】:

    【解决方案2】:

    在您的主目录中,您可以更改为myView = ViewByCountry(docID,user_selection),然后添加print(myView)。这会将您的函数创建的列表保存到一个变量中,以便稍后打印或使用。

    【讨论】:

      猜你喜欢
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 2015-01-13
      相关资源
      最近更新 更多