我有几个大约一百万行的文件
这是文件内容的一个示例:
begin(model(tb4)).
...
sequence_length(187).
amino_acid_pair_ratio(a,a,24.8).
amino_acid_pair_ratio(a,c,0.0).
...
tb_to_tb_evalue(tb3671,1.100000e-01).
tb_to_tb_evalue(tb405,4.300000e-01).
tb_to_tb_evalue(tb3225,5.600000e-01).
...
end(model(tb4))
begin(model(tb56)).
......
end(model(tb56))
通过像这样的输入
myarray = (tb4, tb56..)
我需要计算每个模型中包含多少行类型为“tb_to_tb_evalue”的行
在这种情况下,对于示例文本,输出应为: tb4=3 tb56=0
到目前为止,我已经这样做了,但我意识到我必须读取所有文件,读取次数与len(myarray)相同
def readorfs():
# Declaramos la ruta de la carpeta que almacena los ficheros
path = "data/orfs"
# Recogemos los nombres de los ficheros
all_files = glob.glob(path + "/*.txt")
# Leemos los ficheros line a linea
for filename in all_files:
with open(filename) as f:
lines = f.readlines() # Lee el fichero line a linea
for line in lines:
if line.startswith("begin(model(") and (myarray[i]) in line:
print(line)
以下是我的建议: 首先创建一个字典,其中myarray的所有项都是键,值=0 然后,创建一个处理特定文件的函数。将整个文件作为文本加载,按“(开始(模型(”)将其拆分,并计算“tb_to_tb_evalue”的所有出现次数。将所有结果添加到字典中。 最后对所有文件运行此函数。 见下文:
您也可以在一个函数中运行所有文件,如下所示。在这种情况下,您必须在函数参数中插入myarray:
这里有一个建议,使用来自
collections
的defaultdict
将您的特定模型名称作为键,并将该模型的tb_to_tb_evalue
行数作为值。因为您正在完全读取所有文件,所以查找所有模型的计数没有实际的额外开销。但最终从列表中获取特定型号的计数将是非常简单的因此,它将遍历所有文件,但只遍历一次。最后,要从特定列表(例如
myarray
)中获取所有模型的计数,您可以编写如下内容:相关问题 更多 >
编程相关推荐