简体   繁体   English

如何在Python中使用带有统计信息的值列表

[英]How to use list of values with statistics in python

The code I am using so far is: 到目前为止,我使用的代码是:

import os
import math
import statistics
def main ():
    infile = open('USPopulation.txt', 'r')
    values = infile.read()
    infile.close()
    values = values.split('\n')
    index = 0
    for _ in values:
        values[index] = int(values[index])
    while index < len(values):
        index += 41
    print(values)
 main()

This code gives me the following output which appears to be a list of integer values from the text file that I am using. 此代码为我提供了以下输出,该输出似乎是我使用的文本文件中的整数值的列表。

[151868, '153982', '156393', '158956', '161884', '165069', '168088', '171187', '174149', '177135', '179979', '182992', '185771', '188483', '191141', '193526', '195576', '197457', '199399', '201385', '203984', '206827', '209284', '211357', '213342', '215465', '217563', '219760', '222095', '224567', '227225', '229466', '231664', '233792', '235825', '237924', '240133', '242289', '244499', '246819', '249623']

My tasks is to create a program which shows average change in population during the time period. 我的任务是创建一个显示该时间段内人口平均变化的程序。 The year with the greatest increase in population during the time period. 在此期间人口增长最大的年份。 The year with the smallest increase in population (from the previous year) during the time period. 该时间段内人口增长最小的年份(与上一年相比)。

I am totally lost on the logic for how to make this happen or where to check for resources, my textbook has not been very helpful on this. 我完全不知道如何执行此操作或在哪里检查资源的逻辑,我的教科书对此并没有很大帮助。

For Example: When I add the following code: 例如:当我添加以下代码时:

pop = sum(values)
print(statistics.mean(pop))

I get this error: 我收到此错误:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Your help is greatly appreciated. 非常感谢您的帮助。 Not sure what to do here. 不知道在这里做什么。

There are a couple of flaws in your code but the actual error you're getting comes from the fact that you do not increase your index variable: 您的代码中有几个缺陷,但是您得到的实际错误是由于您没有增加index变量而导致的:

values = list(map(int, values))

would possibly be what you're after (instead of the for loop, that is). 可能就是您想要的(而不是for循环)。 After this, at least the TypeError should be gone. 此后,至少TypeError应该消失了。 Additionally, consider using with instead of opening/closing the file by hand. 此外,考虑使用with代替手动打开/关闭文件。 That being said, you could shorten your whole main to: 话虽这么说,您可以将整个main缩短为:

def main ():
    with open("USPopulation.txt", "r") as infile:
        values = list(map(int, infile.read().splitlines()))
        return values

as @jan mentioned, one of your problem is when you try converting your list of string to list of int. 正如@jan所提到的,您的问题之一是当您尝试将字符串列表转换为int列表时。 you should do it this way: 您应该这样做:

values= [int(i) for i in values]

or the one that @jan said will work too. 或@jan说的也可以。 after that mean operation needs two values, or in this case, it gets a list and uses the length of it as the second value which you did not provide in your code. 之后,该平均操作需要两个值,或者在这种情况下,它获得一个列表并将其长度用作您在代码中未提供的第二个值。 this gives you an average of the population: 这样可以得到平均人口数:

print(statistics.mean(values))

but I think you want the mean of population increase, not just population. 但是我认为您想要的是人口增长的平均值,而不仅仅是人口增长。 in this case, you need to have another list of differences, then calculate the mean of that. 在这种情况下,您需要另一个差异列表,然后计算平均值。

diff=[second-first for first, second in zip(values,values[1:])]

the list "diff" will contain difference values for each consequative years. 列表“ diff”将包含每个相应年份的差异值。 you can do operations like min , max and mean on this list to get what you want. 您可以在此列表上执行minmaxmean等操作,以获取所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM