我有这个值表,我想知道如何让程序读取每一行。对于带有'a','g','c'或'u'的每一行,我希望它将计数增加一。对于这个例子,当我运行它时,它应该有12的结果。
a 1 0.000 S
g 2 0.260 S
a 3 0.990 S
a 4 0.980 S
c 5 0.000 S
u 6 1.000 S
c 7 0.000 S
a 8 1.000 S
a 9 1.000 T
u 10 0.820 S
a 11 1.000 T
g 12 0.000 S
F 13 1.000 S
S 14 1.000 S
T 15 1.000 S
我尝试过的代码如下:
rna_residues = ['a','c','g','u']
count_dict = {}
#Making the starting number 0
rna_count = 0
#if any lines of the file starts with one of the rna_residue
if line.startswith(tuple(rna_residues)):
for residue in line:
if residue in rna_residues:
rna_count += 1
count_dict[line] = [rna_count]
print count_dict
不知何故,当我运行它时,没有计数列表:
{'a 1 0.000 S\n': [1]}
{'g 2 0.260 S\n': [1]}
{'a 3 0.990 S\n': [1]}
{'a 4 0.980 S\n': [1]}
{'c 5 0.000 S\n': [1]}
{'u 6 1.000 S\n': [1]}
{'c 7 0.000 S\n': [1]}
{'a 8 1.000 S\n': [1]}
{'a 9 1.000 T\n': [1]}
{'u 10 0.820 S\n': [1]}
{'a 11 1.000 T\n': [1]}
{'g 12 0.000 S\n': [1]}
我知道这是很多信息,但是有什么提示可以帮助我解决这个问题吗?非常感谢!!
答案 0 :(得分:1)
你使用整行作为字典中的一个键,所以除非你有相同的行,否则所有的值都是1.为什么你需要字典呢?我认为你想要计算以任何一个字符'a','c','g','u'
开头的行数。
为此,以下代码就足够了:
rna_residues = ['a','c','g','u']
rna_count = 0
with open('/path/to/file') as opened_file:
for line in opened_file:
# or if line[0] in rna_residues
if any(line.startswith(residue) for residue in rna_residues):
rna_count += 1
print rna_count
# 12