[英]Add first value for each tuple
I have a nested dictionary inputlist
.我有一个嵌套的字典
inputlist
。 Each list has 2 tuples.每个列表有 2 个元组。 I want to calculate total summation of each
tuple
first value by class 0 and 1.我想按类 0 和 1 计算每个
tuple
第一个值的总和。
Here is my code:这是我的代码:
inputlist={1:{0: [(5.0, 3.6), (20.0, 0.0)],
1: [(4.0, 0.0), (22.0, 0.0)]},
2:{0: [(2.0, 0.5), (21.0, 0.5)],
1: [(5.0, 0.0), (22.0, 0.0)]}
}
sum_result={}
for k1, v1 in inputlist.items():
for (k2, v2) in v1.items():
sum_result[k2]=1
for i in range(len(v2)):
(value1, value2) = v2[i]
sum_result[k2] += value1
print(sum_result)
Output should look like this:输出应如下所示:
{0: 48.0, 1: 53.0}
Here is a solution you can give it a try.这是您可以尝试的解决方案。
from collections import defaultdict
sum_ = defaultdict(int)
for k, v in input_list.items():
for ki, vi in v.items():
sum_[ki] += sum(li[0] for li in vi)
print(sum_)
defaultdict(<class 'int'>, {0: 48.0, 1: 53.0})
You can try this method:你可以试试这个方法:
inputlist={1:{0: [(5.0, 3.6), (20.0, 0.0)],
1: [(4.0, 0.0), (22.0, 0.0)]},
2:{0: [(2.0, 0.5), (21.0, 0.5)],
1: [(5.0, 0.0), (22.0, 0.0)]
}
}
sum_l={}
all_list=[]
for i in inputlist.values():
for j in i.values():
t_sum=0
for k in j:
t_sum+=k[0]
all_list.append(t_sum)
j={0:sum(all_list[::2]),1:sum(all_list[1::2])}
print(j)
You can also try this:你也可以试试这个:
sum_l={}
all_list=[]
sum_={}
for a, b in inputlist.items():
for c, d in b.items():
try:
sum_[c] += sum(li[0] for li in d)
except KeyError:
sum_[c] = sum(li[0] for li in d)
print(sum_)
Here is the code I hope you will understand the code now.这是代码,我希望你现在能理解代码。 Two separate variables for 0 and 1 would make your job much easier.
0 和 1 的两个独立变量将使您的工作更轻松。 When the outer loop is executed fully, you can just initialize your sum_result dictionary with the values updated by the loop.
当外部循环完全执行时,您可以使用循环更新的值初始化 sum_result 字典。
inputlist={1:{0: [(5.0, 3.6), (20.0, 0.0)],
1: [(4.0, 0.0), (22.0, 0.0)]},
2:{0: [(2.0, 0.5), (21.0, 0.5)],
1: [(5.0, 0.0), (22.0, 0.0)]
}}
sum_result={}
sum_of_tuples_for_0=0
sum_of_tuples_for_1=0
for key,val in inputlist.items():
for k1,v1 in val.items():
if k1==0:
for j1,j2 in v1:
sum_of_tuples_for_0+=j1
else :
for j1,j2 in v1:
sum_of_tuples_for_1+=j1
sum_result[0]=sum_of_tuples_for_0
sum_result[1]=sum_of_tuples_for_1
print(sum_result)
Solving it only uses indexing.解决它只使用索引。
inputlist = {1: {0: [(5.0, 3.6), (20.0, 0.0)],
1: [(4.0, 0.0), (22.0, 0.0)]},
2: {0: [(2.0, 0.5), (21.0, 0.5)],
1: [(5.0, 0.0), (22.0, 0.0)]
}
}
sum_result_0 = {}
sum_result = {}
for i in inputlist.items():
if i[0] == 1:
sum_result_0[0] = i[1][0][0][0] + i[1][0][1][0]
sum_result_0[1] = i[1][1][0][0] + i[1][1][1][0]
if i[0] == 2:
sum_result[0] = i[1][0][0][0] + i[1][0][1][0]
sum_result[1] = i[1][1][0][0] + i[1][1][1][0]
sum_result = {0: sum_result[0] + sum_result_0[0], 1: sum_result[1] + sum_result_0[1]}
print(sum_result)
You could try this:你可以试试这个:
sum_result=dict.fromkeys(range(2),0)
for v in inputlist.values():
for k,i in v.items():
sum_result[k] += i[0][0] + i[1][0]
print(sum_result)
Or this或这个
sum_result=dict.fromkeys(range(2),0)
for v in inputlist.values():
sum_result[0] += v[0][0][0] + v[0][1][0]
sum_result[1] += v[1][0][0] + v[1][1][0]
print(sum_result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.