我有一个字典,在多个键下有多个值。我不想要一个值的总和。我想找到一种方法来计算每个键的和。
该文件用制表符分隔,标识符是这两个项的组合,即Btarg。每个标识符都有多个值。
下面是一个测试文件:
下面是测试结果:
图案项目丰度
1蚂蚁2
2只狗10只
3长颈鹿15
1蚂蚁4
2只狗5
以下是预期结果:
图案1,6
图案2dog,15
图案3长颈鹿,15
到目前为止,我得到的是:
for line in K:
if "pattern" in line:
find = line
Bsplit = find.split("\t")
Buid = Bsplit[0]
Borg = Bsplit[1]
Bnum = (Bsplit[2])
Btarg = Buid[:-1] + "//" + Borg
if Btarg not in dict1:
dict1[Btarg] = []
dict1[Btarg].append(Bnum)
#The following used to work
#for key in dict1.iterkeys():
#dict1[key] = sum(dict1[key])
#print (dict1)
在Python3中,如果没有错误消息“Unsupported operand type for+:'int'and'list'? 提前谢谢!在
使用
from collections import Counter
从documentation:
对于你的评论,现在我想我知道你想要什么,尽管我不知道你的数据是什么结构。我认为您可以像这样组织数据:
^{pr2}$首先创建一个defaultdict
那就开始上课吧:
结果:
更新2
假设您可以使用以下格式获取数据:
相关问题 更多 >
编程相关推荐