Python Forum

Full Version: How to find difference between elements in a list? Using beginner Basics only.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have been going crazy on a portion of an assignment, I am hoping to get help with. Only using Basic begginer python coding...Functions, loops, files, lists, modules.

In my assignment I have been given a .txt file with a several number on each line ( the numbers represent population amounts). I need to read this file into a list(list 1, I can do this.) Then I have to make 2 more list, one of a list of years between 1950 and 1990(list 2, I can do this) and then a list which stores the population changes from one year to the next(list 3, Need help with this! where to start? For / in?).

I need help on how to make the the list 3, the difference between the populations. How do you compute the difference between sequential elements in a list?
Here is what I have so far... I wanted to make sure I could compute each list needed first before getting into placing these lists into functions and def them ect.


popfile = open("somenumbers.txt", "r") #open File and read into list population
population = popfile.readlines() #list1 from file
popfile.close()
print()

#make list 2 of years 1950-1991
year = list(range(1950,1991))
print(year)

#List 3 Difference in populations
diff_list=[]
for x in range(1,len(population)):
print(population)
diff_list.append(population[x]-population[x-1])
print("the diff_list",diff_list)

With the above I am getting unsupported operand - for str and str:

The file contains 41 lines with each line having a number which represents a population between 1950-1991. (eventually I will use the list of years I make, so someone can enter a year and have the population returned along with other info. When the .txt file is viewed it is in the following format for 41 total lines.(example numbers)

124499
193284
138479
193849
540392
293485
129384
It would be helpful to see what you have done so far. You should also provide more information about what is in the file.
(Nov-11-2020, 09:11 PM)deanhystad Wrote: [ -> ]It would be helpful to see what you have done so far. You should also provide more information about what is in the file.

Added more details to original post.
Don't add to the original post. Include new details in new posts.

When including code, output or errors be sure to wrap it in the appropriate tags that appear at the top of the editor window. Code posted as regular text loses indentation which is really important in Python.

What does readlines() return? I wrote a little program to find out.
with open("debug.txt", "r") as file:
    values = file.readlines()
    print(type(file))
    print(type(values))
    print(type(values[0]))
Output:
<class '_io.TextIOWrapper'> <class 'list'> <class 'str'>
"open" returns a TextIOWrapper, file.readlines() returns a list, and the list is filled with str.

There's your problem. Reading the file returns strings and you want ints. How can you convert strings to ints? Once you solve that it should be easy to do the math to calculate population change from year to year.
Need a hint... If I use int() I am getting an error. Should I be itterating through the list, say by a For / in statement to turn a list into an integer?
Need a hint... If I use int() I am getting an error. Should I be itterating through the list, using a For / in statement to turn a list into an int?

(Nov-11-2020, 10:59 PM)deanhystad Wrote: [ -> ]Don't add to the original post. Include new details in new posts.

When including code, output or errors be sure to wrap it in the appropriate tags that appear at the top of the editor window. Code posted as regular text loses indentation which is really important in Python.

What does readlines() return? I wrote a little program to find out.
with open("debug.txt", "r") as file:
    values = file.readlines()
    print(type(file))
    print(type(values))
    print(type(values[0]))
Output:
<class '_io.TextIOWrapper'> <class 'list'> <class 'str'>
"open" returns a TextIOWrapper, file.readlines() returns a list, and the list is filled with str.

There's your problem. Reading the file returns strings and you want ints. How can you convert strings to ints? Once you solve that it should be easy to do the math to calculate population change from year to year.
(Nov-16-2020, 08:47 PM)Anklebiter Wrote: [ -> ]Need a hint... If I use int() I am getting an error. Should I be itterating through the list, using a For / in statement to turn a list into an int?

deanhystad explained your problem. If you need additional hint: I suggest to convert values to int while reading the file.
five = '5'
print(int(five)**2)
print(five**2)
Output:
25 Traceback (most recent call last): File "C:\Users\djhys\Documents\python\musings\junk2.py", line 3, in <module> print(five**2) TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
You can square the number 5 but not the string '5'. Same goes for subtraction. However both strings and ints know how to add though the results may surprise you.
print(5+5)
print('5'+'5')
Output:
10 55
Finally Got the INT() in the correct place...and indentations correct. Thanks all!