Christian R
Christian R

Reputation: 313

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.

Upvotes: 2

Views: 1207

Answers (2)

prhmma
prhmma

Reputation: 953

as @jan mentioned, one of your problem is when you try converting your list of string to list of int. you should do it this way:

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

or the one that @jan said will work too. 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. you can do operations like min,max and mean on this list to get what you want.

Upvotes: 3

Jan
Jan

Reputation: 43189

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:

values = list(map(int, values))

would possibly be what you're after (instead of the for loop, that is). After this, at least the TypeError should be gone. Additionally, consider using with instead of opening/closing the file by hand. That being said, you could shorten your whole main to:

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

Upvotes: 2

Related Questions