How to use counters and zip functions with a list of lists in Python?
<p>I have a list of lists :</p>

<pre><code>results = [['TTTT', 'CCCZ'], ['ATTA', 'CZZC']]
</code></pre>

<p>I create a counter that stores number number of characters in each element in each list, only if the characters are ATGC [NOT Z]</p>

<pre><code>The desired output is [[4,3],[4,2]]
</code></pre>

<p>**</p>

<p>Code:</p>

<pre><code>counters = [Counter(sub_list) for sub_list in results]
    nn =[]
    d = []
    for counter in counters:
            atgc_count = sum((val for key, val in counter.items() if key in "ATGC"))    
            nn.append(atgc_count)
d = [i - 1 for i in nn]
correctionfactor = [float(b) / float(m) for b,m in zip(nn, d)]
print nn
print correctionfactor

"Failed" Output:
[0, 0]
&lt;closed file 'c:/test/zzz.txt', mode 'r' at 0x02B46078&gt;

Desired Output
nn = [[4,3],[4,2]]
correctionfactor = [[1.33, 1.5],[1.33,2]]
</code></pre>

<p>**</p>

<p>And then I calculate frequency of each character (pi), square it and then sum (and then I calculate het = 1 - sum). </p>

<pre><code>The desired output [[1,2],[1,2]] #NOTE: This is NOT the real values of expected output. I just need the real values to be in this format. 
</code></pre>

<p>**
Code</p>

<pre><code>list_of_hets = []
for idx, element in enumerate(sample):
    count_dict = {}
    square_dict = {}
    for base in list(element):
         if base in count_dict:
            count_dict[base] += 1
        else:
            count_dict[base] = 1
    for allele in count_dict:
        square_freq = (count_dict[allele] / float(nn[idx]))**2
        square_dict[allele] = square_freq        
    pf = 0.0
    for i in square_dict:
        pf += square_dict[i]   # pf --&gt; pi^2 + pj^2...pn^2
    het = 1-pf                    
    list_of_hets.append(het)
print list_of_hets

"Failed" OUTPUT:
[-0.0, -0.0]
</code></pre>

<p>**
I need to multiply every elements in list_of_hets by correction factor</p>

<pre><code>h = [float(n) * float(p) for n,p in zip(correction factor,list_of_hets)
With the values given above:
h = [[1.33, 1.5],[1.33,2]] #correctionfactor multiplied by list_of_hets 
</code></pre>

<p>Finally, I need to find the average value of every element in h and store it in a new list.</p>

<pre><code>The desired output should read as [1.33, 1.75].
</code></pre>

<p>I tried following this example (<a href="http://stackoverflow.com/questions/13783315/sum-of-list-of-lists-returns-sum-list">Sum of list of lists; returns sum list</a>). </p>

<pre><code>hs = [mean(i) for i in zip(*h)]
</code></pre>

<p>But I get the following error "TypeError: zip argument #1 must support iteration"</p>

<p>I understand that correcting the code at the first step may solve it. I tried to manually input the "desired outputs" and run the rest of the code, but no luck.</p>

<p>First map traverse results.
Second map replace 'Z' and count element.</p>

<pre><code>map(lambda x:map(lambda y:len(y.replace('Z','')),x),l)
</code></pre>

python list error-handling append