擅长:python、mysql、java
<p>只有不包含以<code>></code>开头的内容的行忽略这些:</p>
<pre><code>with open("input.fasta") as ecoli: # will close your file automatically
from collections import defaultdict
counts = defaultdict(int)
for line in ecoli: # iterate over file object, no need to read all contents into memory
if line.startswith(">"): # skip lines that start with >
continue
for char in line: # just iterate over the characters in the line
if char in {"A", "C", "D", "E", "F", "G", "H", "I", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "Y"}:
counts[char] += 1
total = float(sum(counts.values()))
for key,val in counts.items():
print("{}: {}, ({:.1%})".format(key,val, val / total))
</code></pre>
<p>你也可以使用集合。计数器dict as the lines only contain what you interest in the lines:</p>
^{pr2}$