Python 榜单数据分析

Python list data analysing

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

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

我还是个初学者,写这篇文章一直有问题。我希望它没有那么难。

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)