Using temp memory for smart calculating
<p>I have a code where I need to go through lines and sum up index x, if they have a matching index[0]. What would be the best solution to create this? I guess I could have defaultdict and if there is a match in index[0] it would add the values. Is there a way that I could read it line by line, and have the index[0] always at temp memory and if it matches next index[0] it would do the sum up?</p>

<p>This is what I have so far: </p>

<pre><code>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. 
</code></pre>

<p>edit. I'm doing this to sorted list.                    </p>

<p>Thanks in advance</p>

<p>edit2. Since people are having problems understanding me maybe some output would help.
Currently variable <code>this</code> prints:</p>

<pre><code>Fam_c1_1        F       Extractions     02-0419 02-419TK        500     400     200000.0
Fam_c1_1        F       Extractions     5107    5107TK  1475    447.5   660062.5
Fam_c10_1       F       Extractions     5132    5132TK  1555    547.6   851518.0
Fam_c100_1      M       Extractions     5843    5843TK  2605    398.6   1038353.0
Fam_c1000_1     F       Extractions     9913    9913TK  1900    398     756200.0
Fam_c1001_1     F       Extractions     9512    9512TK  1050    20      21000.0
</code></pre>

<p>So in this case I'd like my code to go through the list, have the first value of the list in the memory all the time. If it matches next lines first value it would do x.</p>
