Python-按年份标记列表中的项目

Python-按年份标记列表中的项目,python,Python,基本上,我正在编写一个程序,读取一个文本文件,并将文件中的每一行放入一个列表中。该文件列出了1950-1990年间美国的人口,以千为单位。但是,该文件没有列出年份,因此我需要我的程序在每个人口数字上标注年份 我的任务是: “您需要编写一个程序(名为Analysis_population.py),将上述文件的内容读入一个列表,并计算和显示: 1950年至1990年(含)期间人口的年均变化 1950-1990年期间人口增长最大的年份(含) 1950-1990年(含1950-1990年)期间人口增长

基本上,我正在编写一个程序,读取一个文本文件,并将文件中的每一行放入一个列表中。该文件列出了1950-1990年间美国的人口,以千为单位。但是,该文件没有列出年份,因此我需要我的程序在每个人口数字上标注年份

我的任务是:

“您需要编写一个程序(名为Analysis_population.py),将上述文件的内容读入一个列表,并计算和显示:

  • 1950年至1990年(含)期间人口的年均变化

  • 1950-1990年期间人口增长最大的年份(含)

  • 1950-1990年(含1950-1990年)期间人口增长最小的年份”

除了年份标签,我的一切都正常

我的代码如下:

year = 1950

with open("USPopulation.txt", "r") as f:
    popList = [line.strip() for line in f]
    popList = [ int(x) for x in popList ]
for x in popList:
    year = year + 1

diff = [abs(j-i) for i,j in zip(popList, popList[1:])]
maxDiff = max(abs(x - y) for (x, y) in zip(popList[1:], popList[:-1]))
minDiff = min(abs(x - y) for (x, y) in zip(popList[1:], popList[:-1]))

print("The average annual change in population during the time period 1950 - 1990 is:\n",
          (sum(diff)/len(diff)))

print("\nThe year with the greatest increase in population during the time period 1950 - 1990 is:",
          year, "-", year + 1, "with an increase of", maxDiff, "people.")

minDiff = min(abs(x - y) for (x, y) in zip(popList[1:], popList[:-1]))
print("\nThe year with the smallest increase in population during the time period 1950 - 1990 is:",
          year, "-", year + 1, "with an increase of", minDiff, "people.")
我得到的结果是:

The average annual change in population during the time period 1950 - 1990 is:
 2443.875

The year with the greatest increase in population during the time period 1950 - 1990 is: 1991 - 1992 with an increase of 3185 people.

The year with the smallest increase in population during the time period 1950 - 1990 is: 1991 - 1992 with an increase of 1881 people.
正如您所看到的,它每次都输出相同、不正确的年份。 如有任何帮助,请予以纠正,我们将不胜感激


我们实际上无法访问该文本文件,它位于登录墙后面。Canvas不是公共访问站点。另外,请阅读张贴指南:您的问题必须是独立的,以便正确存档。文本文件已修复