3 ms·
One liner to count gc, without buffering. import io f = io.StringIO( """ AB CD EF GH """ ) total = sum(map(lambda s: 0
by user5994461 5y ago
One liner to count gc, without buffering.
import io
f = io.StringIO(
"""
AB
CD
EF
GH
"""
)
total = sum(map(lambda s: 0 if s[0]==">" else s.count('G') + s.count('C'), f.readlines()))
print(total)
- user5994461 5y agoAnd reading the file as binary. There's a lesson about the overhead of unicode strings here ;) Your first example takes 3.1 seconds, my previous comment takes 2.3 seconds, this one takes 1.4 seconds. start = time.perf_counter() with open("orthocoronavirinae.fasta", "rb") as f: total = sum(map(lambda s: 0 if s[0]==65 else s.count(b"G") + s.count(b"C"), f.readlines())) end = time.perf_counter() print(total, " total") print(end-start, " seconds")
- soundmasterj 5y agoThis is good too. I would use a generator expression instead of the map though probably.
- user5994461 5y agomap is a generator