计算具有特定字符的行后的行

时间:2015-05-23 16:33:52

标签: python

我有一个包含此数据的文件:

>P136
FCF#0.73
FCF#0.66
FCF#0.86
>P129
FCF#0.72
>P142
>P144
>P134
FCF#0.70
FCF#0.82

我需要计算包含“>”的行后面的行数,但保持“>”作为参考的行,对于此示例,输出应为:

>P136 3
>P129 1
>P134 2

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

使用字典存储每行的计数,每次开始时都没有memoryview,增加计数:

>

然后简单地循环并打印计数:

counts = {}
current = None

with open(filename) as fo:
   for line in fo:
       if line.startswith('>'):
           current = line.strip()
           counts[current] = 0
       else:
           counts[current] += 1

您甚至可以在每次找到新栏目时打印该号码:

for entry, count in counts.items():
    print('{} {:2d}'.format(entry, count))

但你不能轻易地重新计算其他工作的计数。

答案 1 :(得分:0)

在一行中,只是为了表明我们可以:

s=""">P136
FCF#0.73
FCF#0.66
FCF#0.86
>P129
FCF#0.72
>P142
>P144
>P134
FCF#0.70
FCF#0.82
"""

第一个变种:

print [(i.split("\n")[0],len(i.split("\n")[1:])-1) for i in s.split(">")if i if len(i.split("\n")[1:])-1>0]

使用re:

import re
print [ (block.split("\n")[0],sum(1 for m in re.finditer("#", block)))for block in s.split(">")]

答案 2 :(得分:0)

这是一个试图简约的简单解决方案。

with open(filename) as f:
    def printcc(current, count):
        if current is not None and count > 0:
            print(current.strip(), count)
    current = None
    count = 0
    for line in f:
        if line[0] == '>':
            printcc(current, count)
            current = line
            count = 0
        else:
            count += 1
    printcc(current, count)

如果您确实想要包含一个>字符的所有行,请使用'>' in line作为条件。如果您的目标是Python 2.x,请使用print current.strip(), count,因为使用外部括号将打印出两个元组。

相关问题