Python Forum
Reading Multiple Lists Using SUM function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading Multiple Lists Using SUM function
#1
Hello,

Here is the code I am trying to improve - can longer lists be read from a file and looped through and printed?

p8262 = [331, 83, 112, 111, 21, 67, 8] 
p8264 = [384, 84, 119, 74, 28, 51, 6] 
p8401 = [330, 114, 217, 81, 11, 52, 5] 

# variables to be looped 

Sum = sum(p8262) 
Sum1 = sum(p8264) 
Sum2 = sum(p8401) 

print("The sum of p8262 is " , Sum) 
print("The sum of p8264 is " , Sum1) 
print("The sum of p8401 is " , Sum2)


All the Best,
Dave
Reply
#2
you can do all in one like:
print(f"The sum of p8262 is {sum(p8262)}")
Reply
#3
Thanks! That works. Is there a way to read and loop through a file? Have around 300 lists.
Reply
#4
what is the file - json? show what you have
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
It's a text file with 259 identical lists like so.

p8262 = [331, 83, 112, 111, 21, 67, 8]
p8264 = [384, 84, 119, 74, 28, 51, 6]
p8401 = [330, 114, 217, 81, 11, 52, 5]

...

print(f"The sum of p8262 is {sum(p8262)}")

Can just the name of the lists be assigned as a variable then I could print out the list name and sum return.

Best
Reply
#6
this is weird file format.
you can do something like
from ast import literal_eval

with open(path_to_your_file) as f:
    for line in f:
        name, values =  line.split(' = ')
        values = literal_eval(values)
        print(f"The sum of {name} is {sum(values)}")
of course, instead of ast.literal_eval, you can parse the list yourself, i.e. after the split at =, you can remove the brackets, split at comma, convert numbers to int, and so on...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Bingo buran - thanks all, BTW this is the coolest python forum. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,431 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  function return boolean based on GPIO pin reading caslor 2 1,130 Feb-04-2023, 12:30 PM
Last Post: caslor
  Use ranking function for different lists klatlap 6 1,966 Feb-15-2022, 11:31 PM
Last Post: klatlap
Sad Iterate randint() multiple times when calling a function Jake123 2 1,979 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Help with WebSocket reading data from anoter function korenron 0 1,300 Sep-19-2021, 11:08 AM
Last Post: korenron
  Pop function for lists jamesaarr 8 2,576 Aug-26-2021, 06:40 PM
Last Post: ndc85430
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,756 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Create Dict from multiple Lists with duplicate Keys rhat398 10 3,979 Jun-26-2021, 11:12 AM
Last Post: Larz60+
  Reading Multiple text Files in pyhton Fatim 1 1,885 Jun-25-2021, 01:37 PM
Last Post: deanhystad
  Function - Return multiple values tester_V 10 4,316 Jun-02-2021, 05:34 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

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