How to calculate counts and frequencies for pairs in list of lists?
<p>Bases refers to A,T,G and C</p>

<pre><code>sample = [['CGG','ATT'],['GCGC','TAAA']]

# Note on fragility of data: Each element can only be made up only 2 of the 4 bases.  
# [['CGG' ==&gt; Only C and G,'ATT' ==&gt; Only A and T],['GCGC'==&gt; Only C and G,'TAAA' ==&gt; Only T and A]]
# Elements like "ATGG" are not present in the data as the have more than 3 different types of bases
</code></pre>

<p>Consider the first pair : ['CGG','ATT']</p>

<ol>
<li><p>Calculate frequency of each base in the pairs separately:</p>

<p>CGG => (C = 1/3, G = 2/3)
 ATT => (A = 1/3, T = 2/3)</p></li>
<li><p>Calculate frequency of occurrence of combination of bases in the pairs. Here, the combinations are 'CA' and 'GT' (Notice, order of the base matters. It is not 'CA','AC','GT' and 'TG'. Just only 'CA' and 'GT'). </p>

<p>Pairs => (CA = 1/3, GT = 2/3) </p></li>
<li><p>Calculate float(a) = (freq of Pairs) - ((freq of C in CGG) * (freq of A in ATT))</p>

<p>Eg in CA pairs, float (a) = (freq of CA pairs) - ((freq of C in CGG) * (freq of A in ATT))</p>

<p>Output a = (1/3) - ((1/3) * (1/3)) = 0.222222</p></li>
</ol>

<p>Calculating "a" for any one combination (either CA pair or GT pair)</p>

<p>NOTE: If the pair is AAAC and CCCA, the freq of C would it be 1/4, i.e. it is the frequency of the base over one of the pairs</p>

<ol start="4">
<li><p>Calculate b
 float (b) = (float(a)^2)/ (freq of C in CGG) * (freq G in CGG) * (freq A in ATT) * (freq of T in ATT)</p>

<pre><code>Output b = 1
</code></pre></li>
</ol>

<p>Do this for the entire list</p>

<pre><code>   Final Output a = [0.2222, - 0.125]
                b = [1, 0.3333]
</code></pre>

<p>This code has been adapted from <a href="https://stackoverflow.com/questions/40066439/how-to-calculate-frequency-of-elements-for-pairwise-comparisons-of-lists-in-pyth?noredirect=1#comment67422112_40066439">this answer</a>. Please note that there are subtle differences in the two questions and they are NOT the same, in the approach to the problem.  </p>

<p>However, I am unable to get this code to run. I get the following error:
   for pair, count in i:
TypeError: 'int' object is not iterable</p>

<pre><code>#Count individual bases.

sample4 = [['CGG','ATT'],['GCGC','TAAA']]
base_counter = Counter()
for i in enumerate(sample4):
    for pair, count in i:
        base_counter[pair[0]] += count
        base_counter[pair[1]] += count
        print base_counter

# Get the total for each base.
total_count = sum(base_counter.values())

# Convert counts to frequencies.
base_freq = {}
for base, count in base_counter.items():
    base_freq[base] = count / total_count
# Not sure how to write a code to count the number of pairs (Step 2)
# Let's say the counts have been stored in pair_counts

# Examine a pair from the two unique pairs to calculate float_a.
for i in enumerate(sample4):
    float(a) = (pair_count[pair] / sum(pair_count.values())) - (base_freq[pair[0]] * base_freq[pair[1]])

# Step 7!
for i in enumerate(sample4):
    float_b = float_a / float(base_freq[0][0] * base_freq[0][1] * base_freq[1][0] * base_freq[1][1])
</code></pre>