如何增加脚本读取行的计数?
How to increase count as script reads lines?
我有这个 table 个值,我想知道如何让程序读取每一行。对于带有 '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]}
我知道这是很多信息,但是有什么提示可以帮助我吗?非常感谢!!
您正在使用整行作为字典中的关键字,因此除非您有相同的行,否则所有值都将为 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
我有这个 table 个值,我想知道如何让程序读取每一行。对于带有 '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]}
我知道这是很多信息,但是有什么提示可以帮助我吗?非常感谢!!
您正在使用整行作为字典中的关键字,因此除非您有相同的行,否则所有值都将为 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