我有一个代码,如果它们具有匹配的index [0],则需要遍历并总结索引x。什么是创建此的最佳解决方案?我想我可能有defaultdict,如果index [0]中有一个匹配项,它将添加这些值。有没有一种方法可以逐行读取它,并且将index [0]始终保留在临时内存中,如果它与下一个index [0]相匹配,它将进行总和?
这是我到目前为止的内容:
with open("test.txt") as f:
dic = defaultdict(list)
for line in f:
spl =line.split("\t")
if("Fam" in line):
dic[spl[0]].append(spl[1:])
a = float(spl[5])
b = float(spl[6])
sum = a * b
output = str(sum)
this = line.strip() + "\t"+output
if("TK" in line): #I would like to start sum up after this. Read all lines that include "TK", check index[0] for matches, if match sum up.
MYYA
相关分类