【问题标题】:How to increase count as script reads lines?如何在脚本读取行时增加计数?
【发布时间】:2017-06-28 06:46:42
【问题描述】:

我有这个值表,我想知道如何让程序读取每一行。对于带有“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]}

我知道这是很多信息,但是有什么提示可以帮助我吗?非常感谢!!

【问题讨论】:

    标签: python-2.7 math listings


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-07
      相关资源
      最近更新 更多