0

I have the following problem.

I need to read x number of text files and get the sum of every third list. An example would be:

file 1

[1.0, 2.0, 3.0]
[3.1, 2.1, 2.1]
[3.4, 3.4, 4.4]

file 2

[1.0, 2.0, 3.0]
[3.1, 2.1, 2.1]
[3.4, 3.4, 4.4]

result:

[2.0, 4.0, 6.0]
[6.2, 4.2, 4.2]
[6.8, 6.8, 8.8]

The question is how to get the matching lists to sum?

I know I can use zip to get sum of lists and I get the files opened, and read by line and stored into lists, but I'm not sure how to target the right ones to sum?

If the case was only for 2, I could check with an if condition if the counter is even or odd, but how to handle it when there are 3 cases? Does Python have a solution built in there? The solution also has to be scalable to an x amount of files containing 3 lists. This also means I cant just make a list of cases.

3
  • 1
    Is there anything prohibiting you from making a list of lists? So that each file you open will ouptut 1 lists containing 3 lists. That way, you can simply use len() to scale up and down for n lists. And summing them can simply be done using a for loop iterator with your desired operation Commented Dec 4, 2019 at 10:53
  • yea, nothing prohibits me from doing that, but how do i sum up for example first with 4th and 7th, 2nd with 5th and 8th, 3rd with 4th and 9th. This is the main problem i have here i cant get an solution for Commented Dec 4, 2019 at 12:16
  • 1
    Then you have len of both nested lists. This is an index and a counter. Then you simply generate your pairs as required. To separate the lists to corresponding lists to sum, i would think of using the modulo function. Commented Dec 4, 2019 at 12:59

2 Answers 2

2
result = list()
for l_index, num_list in enumerate(file1):
    result_list = list()
        for e_index, element in enumerate(num_list):
            result_list.append(file1[l_index][e_index] + file2[l_index][e_index])
result.append(result_list)

I just assumed every file is saved in a nested list, if this is not the case i don't really know how you should do it.

I'm not sure if 1. the code I wrote is correct (but you should get what i was doing) and 2. if i understood right what you were trying to do.

EDIT: Adjusted the code so now it does what it should

Sign up to request clarification or add additional context in comments.

5 Comments

no thats not really it. if i understood your code right, you are combining the lists here. I need every third list i read from the files to be summed with the corresponding one, like first with 4th and 7th, 2nd with 5th and 8th, 3rd with 4th and 9th basically i need to end up with 3 lists containing the sums of the matching ones
@Chakson I'm confused and pretty sure that unfortunately I can't help you, but for clarification what my code does: Assuming every file is a nested list (list of lists), it takes: the first value from the first list in the first file and addes this number to: the first value from the first list in the second file and so on, and this should look something like this: file1 [[1.0, 2.0, 3.0], [1.1, 2.1, 3.1], [1.4, 2.4, 3.4]] file2 [[4.0, 5.0, 6.0], [4.1, 5.1, 6.1], [4.4, 5.4, 6.4]] result: [[5.0, 7.0, 9.0], [5.2, 7.2, 9.2], [5.8, 7.8, 9.8]]
Well yes basically it does what was intended. I will accept the answer, even if it does not fit my needs as it is a solution. Problem is im looking at a whole directory using os.listdir(path) so i cannot really differentiate file1, file2 and so on in one run. I ended up making a big list and using list[0::9] to get the matching elements and so on.
Chakson do look at my solution. I think thats more in line with what you are looking for
Also viable, thanks a lot! i ended up doing what i wrote there as it works for my use case
1

Would this work?

Given a nested list as in your case problem:

file1 = [[1.0,2.0,3.0],[3.1,2.1,2.1],[3.4,3.4,4.4]]
file2 = [[1.0,2.0,3.0],[3.1,2.1,2.1],[3.4,3.4,4.4]] 

simply concat the nested lists after n files. (order remains so we are good)

comb = file1+file2

Perform your operations in a function so its "modular"

def operate(data,n):#heres the function
    if n == 2: #naive way to initialize your output
        summary = [[],[]]
    elif n == 3:
        summary = [[],[],[]]
    for index,dat in enumerate(data): #getting index +the list
        f = [] #holder for the temp data. habit of overwriting stuff
        if index%n == 1: #modulo function lets you do for every n list. 
                         #Good experiment to extend this dynamically for n cases instead of just up to 3
            if len(summary[1]) == 0:
                summary[1] = dat
            else:
                for a,b in zip(summary[1],dat): #your zip 
                    f.append(a+b)
                summary[1] = f #since its a sum we just do it for each pair and replace 


        elif index%n == 2: 
            if len(summary[2])== 0:
                summary[2] = dat
            else:
                for a,b in zip(summary[2],dat):
                    f.append(a+b)
                summary[2] = f
        elif index%n == 0:
            if len(summary[0])== 0:
                summary[0] = dat
            else:
                for a,b in zip(summary[0],dat):
                    f.append(a+b)
                summary[0] = f



    return summary
file1 = [[1.0,2.0,3.0],[3.1,2.1,2.1],[3.4,3.4,4.4]]
file2 = [[1.0,2.0,3.0],[3.1,2.1,2.1],[3.4,3.4,4.4]] 

comb = file1+file2
t2 = operate(comb,2)
t3 = operate(comb,3)

print("for every second list sum: ",t2)
print("for every third list sum: ",t3)

Theoretically you can extend this for any set by either programming the cases dynamically but you get the gist of it I think.

Output:

for every second list sum:  [[7.5, 7.5, 9.5], [7.5, 7.5, 9.5]]
for every third list sum:  [[2.0, 4.0, 6.0], [6.2, 4.2, 4.2], [6.8, 6.8, 8.8]]

Do try to initialize the summary variable nicer. It will be part of your solution for extending the cases to orders >3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.