【问题标题】:Python list data analysingPython列表数据分析
【发布时间】:2015-01-11 14:14:21
【问题描述】:

我需要在 python 中编写一个小程序,我必须在其中分析列表中的数据。 列表有这些列:国家、年份、男性移民、男性移民、女性移民、女性移民

我要做的是首先询问用户他们想要获取哪个国家/地区的信息。程序需要获得多年来的平均移民人数,并打印出移民何时超过平均水平。最后,我必须打印出一个表格,上面有国家名称和移民超过移民的年份。

我还是个初学者,写这篇文章时遇到了麻烦。我希望它没有那么难。

【问题讨论】:

  • 请分享你的麻烦和你做了什么。

标签: python list average


【解决方案1】:
data = [
    # some made-up numbers for illustration
    ("France", 2006, 64026, 72527, 87442, 98435),
    ("France", 2007, 82051, 75285, 97193, 69527),
    ("France", 2008, 88827, 51960, 77807, 81725),
    ("Germany", 2006, 85653, 83989, 66603, 66559),
    ("Germany", 2007, 85431, 83590, 88482, 87827),
    ("Germany", 2008, 67184, 96350, 84947, 67874),
    ("Greece", 2006, 72062, 55844, 51758, 53004),
    ("Greece", 2007, 58566, 62006, 65323, 80320),
    ("Greece", 2008, 71298, 69158, 74774, 93267)
]

def average(lst):
    return sum(lst) / (len(lst) or 1)

def main():
    # prompt for country
    country = input("For which country? ")

    # filter data by specified country
    country_data = [row for row in data if row[0]==country]

    # get average yearly emigration
    avg_emigration = average([row[3] + row[5] for row in country_data])

    # print years in which emigration exceeds average
    print("Above-average emigration in:")
    for row in country_data:
        if row[3] + row[5] >= avg_emigration:
            print(row)

    # I'll leave the last bit for you to figure out;
    # very similar to the preceding clause

if __name__ == "__main__":
    main()

运行方式

>>> main()
For which country? France
Above-average emigration in:
('France', 2006, 64026, 72527, 87442, 98435)

【讨论】:

    猜你喜欢
    • 2018-06-11
    • 2011-06-12
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    相关资源
    最近更新 更多