gpt4 book ai didi

python - 如何使这个结束修剪 python 脚本更快?

转载 作者:太空宇宙 更新时间:2023-11-04 07:23:40 24 4
gpt4 key购买 nike

有什么建议可以让这个脚本运行得更快?对于这个脚本,我通常有两到一千万行以上。

while True:
line=f.readline()
if not line:break
linker='CTGTAGGCACCATCAAT'
line_1=line[:-1]
for i in range(len(linker)):
if line_1[-i:]==linker[:i]:
new_line='>\n'+line_1[:-i]+'\n'
seq_1.append(new_line_1)
if seq_2.count(line)==0:
seq_2.append(line)
else:pass
else:pass

最佳答案

首先,您似乎在内循环中创建了很多字符串对象。您可以先尝试构建前缀列表:

linker = 'CTGTAGGCACCATCAAT'
prefixes = []
for i in range(len(linker)):
prefixes.append(linker[:i])

此外,您可以使用 string 的方法 endswith 而不是在内部循环的条件中创建新对象:

while True:
line=f.readilne()
if not line:
break
for prefix in prefixes:
if line_1.endswith(prefix):
new_line='>\n%s\n' % line_1[:-len(prefix)]
seq_1.append(new_line_1)
if seq_2.count(line)==0:
seq_2.append(line)

我不确定那里的索引(比如 len(prefix))。也不知道它能快多少。

关于python - 如何使这个结束修剪 python 脚本更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10473653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com