[英]Python sum of every third list
I have the following problem.我有以下问题。
I need to read x number of text files and get the sum of every third list.我需要读取 x 个文本文件并获取每三个列表的总和。 An example would be:
一个例子是:
file 1文件 1
[1.0, 2.0, 3.0]
[3.1, 2.1, 2.1]
[3.4, 3.4, 4.4]
file 2档案 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?我知道我可以使用
zip
来获取列表的总和,然后打开文件,逐行读取并存储到列表中,但我不确定如何定位正确的总和?
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?如果案例只有 2,我可以使用 if 条件检查计数器是偶数还是奇数,但是当有 3 个案例时如何处理? Does Python have a solution built in there?
Python 有内置的解决方案吗? The solution also has to be scalable to an x amount of files containing 3 lists.
该解决方案还必须可扩展到包含 3 个列表的 x 数量的文件。 This also means I cant just make a list of cases.
这也意味着我不能只列出案例。
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.我不确定 1. 我写的代码是否正确(但你应该明白我在做什么)和 2. 如果我理解你想要做什么。
EDIT: Adjusted the code so now it does what it should编辑:调整了代码,现在它做了它应该做的
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.只需在 n 个文件之后连接嵌套列表。 (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
它将成为您将案例扩展到订单 >3 的解决方案的一部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.