Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何附加到多维列表_Python - Fatal编程技术网

Python 如何附加到多维列表

Python 如何附加到多维列表,python,Python,我该怎么做?我拉了一个int值列表,并试图将年份添加到列表中 dataList = [] read = open("USPopulation.txt", 'r') population = read.readline().rstrip('\n') year = 1950 i = 0 maxPopulation = 0 minPopulation = sys.maxsize avgPopulation = 0 total = 0 maxPop_Year = 0 minPop_Year = 0

我该怎么做?我拉了一个int值列表,并试图将年份添加到列表中

dataList = []
read = open("USPopulation.txt", 'r')
population = read.readline().rstrip('\n')

year = 1950
i = 0
maxPopulation = 0
minPopulation = sys.maxsize
avgPopulation = 0
total = 0
maxPop_Year = 0
minPop_Year = 0

while population != "":
        year += i
        dataList[i].append(year)
        dataList[i].append(population)
        i += 1
        population = read.readline().rstrip('\n')
错误

在我处理信息输出之前,我试图先运行它。但我打算做的是处理这些信息,找出人口的最大数量和最小数量,以及各年之间的平均数量,并显示这些信息

    for i in range(len(dataList)):
    if dataList[i][1] > maxPopulation:
        maxPopulation = dataList[i][1]
        macPop_Year = dataList[i][0]
    if dataList[i][1] < minPopulation:
        minPopulation = dataList[i][1]
        minPop_Year = dataList[i][0]        

print("The year", maxPop_Year, "had the most population with", maxPopulation)
print("The year", minPop_Year, "had the least population with", minPopulation)

min、max和avg的完整代码(但使用内联列表而不是文件读取器) 您应该使用哈希表或关联数组,而不是列表。 语句
year+=i
也不正确,因为它将使
year
以一种意外的方式增加--1950、1951、1953、1956、1960

dataList = {}
input = [151868,153982,156393,158956,161884,165069,168088,171187,174149,177135,179979,182992,185771, 188483]
year = 1949
min_popl = -1
max_popl = -1
minyr = -1
maxyr = -1
total_popl = 0
i = 0
for popl in input:
    year += 1
    i += 1
    dataList[year] = popl

    total_popl += popl

    if min_popl == -1 or popl < min_popl:
      min_popl = popl
      minyr = year

    if popl > max_popl:
      max_popl = popl
      maxyr = year

print("Minimum popl [{}] in year [{}]".format(min_popl, minyr))
print("Maximum popl [{}] in year [{}]".format(max_popl, maxyr))
print("Average popl [{}]".format(total_popl/i))

请提供示例输入、该输入的预期输出,以及使用该输入运行代码时遇到的任何错误。欢迎使用SO,请提供更多详细信息,以便人们正确研究您的问题。添加了更多信息DataList.append([])
    for i in range(len(dataList)):
    if dataList[i][1] > maxPopulation:
        maxPopulation = dataList[i][1]
        macPop_Year = dataList[i][0]
    if dataList[i][1] < minPopulation:
        minPopulation = dataList[i][1]
        minPop_Year = dataList[i][0]        

print("The year", maxPop_Year, "had the most population with", maxPopulation)
print("The year", minPop_Year, "had the least population with", minPopulation)
if dataList[i][1] > maxPopulation:
TypeError: '>' not supported between instances of 'str' and 'int'
dataList = []
read = open("USPopulation.txt", 'r')
population = read.readline().rstrip('\n')
year = 1950
i = 0
while population != "":
    year += i
    #An empty list needs to be append to add items
    #fix begin
    dataList.append([])
    #fix end
    dataList[i].append(year)
    dataList[i].append(population)
    i += 1
    population = read.readline().rstrip('\n')
dataList = {}
input = [151868,153982,156393,158956,161884,165069,168088,171187,174149,177135,179979,182992,185771, 188483]
year = 1949
min_popl = -1
max_popl = -1
minyr = -1
maxyr = -1
total_popl = 0
i = 0
for popl in input:
    year += 1
    i += 1
    dataList[year] = popl

    total_popl += popl

    if min_popl == -1 or popl < min_popl:
      min_popl = popl
      minyr = year

    if popl > max_popl:
      max_popl = popl
      maxyr = year

print("Minimum popl [{}] in year [{}]".format(min_popl, minyr))
print("Maximum popl [{}] in year [{}]".format(max_popl, maxyr))
print("Average popl [{}]".format(total_popl/i))
Minimum popl [151868] in year [1950]
Maximum popl [188483] in year [1963]
Average popl [169709.7142857143]