counting occurrence of elements in each sequence of the list in python
<p>I have a (very big) list like the small example. I want to count the number of D in each sequence in the list and divide by the length of that sequence. (occurrence of D in each sequence).</p>

<p>small example:</p>

<pre><code>l = ['MLSLLLLDLLGLG', 'MEPPQETNRPFSTLDD', 'MVDLSVSPDVPKPAVI', 'XNLMNAIMGSDDDG', 'MDRAPTEQNDDVKLSAE']
</code></pre>

<p>do you guys know how to do that?</p>

<p>You can use list comprehension in order to get the expected result.</p>

<p>I have iterated over each item in the list, for each one of the items in the list I've counted the number of the occurrences of the specified sub string (in this case 'D').</p>

<p>Last, I've divided the number of the occurrences with the length of the item.</p>

<pre><code>l = ['MLSLLLLDLLGLG', 'MEPPQETNRPFSTLDD', 'MVDLSVSPDVPKPAVI', 'XNLMNAIMGSDDDG', 'MDRAPTEQNDDVKLSAE']
output = [float(item.count("D")) / float(len(item)) for item in l]
</code></pre>

python