Python Forum
How to find difference between elements in a list? Using beginner Basics only.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find difference between elements in a list? Using beginner Basics only.
#1
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
Reply
#2
It would be helpful to see what you have done so far. You should also provide more information about what is in the file.
Reply
#3
(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.
Reply
#4
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.
Reply
#5
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?
Reply
#6
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.
Reply
#7
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
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
Reply
#9
Finally Got the INT() in the correct place...and indentations correct. Thanks all!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with adding duplicates elements together in a list 2ECC3O 5 2,026 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  Unexpected behavior accessing list elements. tonyflute 2 2,259 Apr-09-2021, 02:36 PM
Last Post: tonyflute
  Help with argument basics mickandralphscrier 5 2,529 Feb-17-2021, 10:11 PM
Last Post: mickandralphscrier
  Get 5 most unique combinations of elements in a 2D list wanttolearn 1 2,310 Sep-24-2020, 02:26 PM
Last Post: buran
  To find the index of the first occurrence of the key in the provided list Angry_bird89 4 3,251 Jun-20-2020, 06:53 PM
Last Post: Angry_bird89
  Loop through elements of list and include as value in the dictionary Rupini 3 2,644 Jun-13-2020, 05:43 AM
Last Post: buran
  How do you find the length of an element of a list? pav1983 13 4,896 Jun-13-2020, 12:06 AM
Last Post: pav1983
  Compare elements of tuples / Find edges between nodes Daniel94 1 1,786 Apr-06-2020, 03:32 PM
Last Post: deanhystad
  How can I print the number of unique elements in a list? AnOddGirl 5 3,264 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Find first letter from a list DariusCG 3 2,508 Nov-27-2019, 11:08 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020