多个字典中的平均值?

问题描述 投票:0回答:1

我有4个字典,其中以symbol作为键,以LTP作为值。现在,我想创建一个新字典,在该字典中,我想将symbol作为我的键,并希望将4个字典的LTP的平均值作为我的值

first = {"MRF":40000,"RELIANCE":1000}
second = {"MRF":50000,"RELIANCE":2000}
third = {"MRF":30000,"RELIANCE":500}
fourth = {"MRF":60000,"RELIANCE":4000}

new = {"MRF":45000,"RELIANCE":1875}  # this is the average of ltp

请协助我解决问题吗?

python
1个回答
0
投票
first = {"MRF":40000,"RELIANCE":1000}
second = {"MRF":50000,"RELIANCE":2000}
third = {"MRF":30000,"RELIANCE":500}
fourth = {"MRF":60000,"RELIANCE":4000}

dicts = [first, second, third, fourth]
keys = first.keys()
new = {k: sum((d[k] for d in dicts)) / len(dicts) for k in first.keys()}
print(new)
热门问题
推荐问题
最新问题