Python 如何从特定行后两行的所有行中计算一个单词?

Python 如何从特定行后两行的所有行中计算一个单词?,python,Python,所以,这听起来可能有点混乱,我会尽力解释。例如,从这些行: next line 1 ^^^^^^^^^^^^^^^^^^ red blue dark ten lemon next line 2 ^^^^^^^^^^^^^^^^^^^ hat 45 no dad fate orange next line 3 ^^^^^^^^^^^^^^^^^^^ tan rat lovely lemon eat you him lemon Daniel her" 我只对上面有两行“next line”的行中

所以,这听起来可能有点混乱,我会尽力解释。例如,从这些行:

next line 1
^^^^^^^^^^^^^^^^^^
red blue dark ten lemon
next line 2
^^^^^^^^^^^^^^^^^^^
hat 45 no dad fate orange
next line 3
^^^^^^^^^^^^^^^^^^^
tan rat lovely lemon eat 
you him lemon Daniel her"
我只对上面有两行“next line”的行中的“lemon”计数感兴趣。所以,我期望的输出是“2个柠檬”

任何帮助都将不胜感激

到目前为止,我的努力是:

#!/usr/bin/env python
#import the numpy library
 import numpy as np

  lemon = 0

  logfile = open('file','r')

  for line in logfile:

  words = line.split()

  words = np.array(words)
  if np.any(words == 'next line'):
    if np.any(words == 'lemon'):
        lemon +=1
print "Total number of lemons is %d" % (lemon)

但只有当“lemon”与“next line”在同一行时,它才算“lemon”。

对于每一行,您需要能够访问它前面的两行。为此,您可以使用
itertools.tee
来创建两个独立的文件对象(类似迭代器的对象),然后使用
itertools.izip()
来创建预期的对:

from itertools import tee, izip
with open('file') as logfile:
    spam, logfile = tee(logfile)
    # consume first two line of spam
    next(spam)
    next(spam)
    for pre, line in izip(logfile, spam):
        if 'next line' in pre:
             print line.count('lemon')
或者,如果只想计算行数,可以在
sum()中使用生成器表达式


只要在文件(这是一个迭代器)上循环,只要找到
下一行
行,就可以调用
next
两次,然后
count
显示
lemon
的频率,其中
for
循环和对
next
的调用都使用相同的迭代器

with open("data.txt") as f:
    lemon_count = 0
    for line in f:
        if "next line" in line:
            next(f) # skip next line
            lemon_count += next(f).count("lemon") # get count for next-next line

例如,
lemon\u count
2
结尾。这是假设在
next
行和
lemon
行之间没有其他
next
行,或者
lemon
行本身是
next
行。

在您的示例中,搜索条件根本不匹配。”“下一个”后面永远不会跟“柠檬”下面的两行。是的,是的。第1行-下一行,第2行-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^。你发布的文本每行之间都有额外的换行符。嗨,托比亚斯,我可以对上面的两行而不是下面的两行做同样的操作吗?@EmilyT。上面的两个更难,因为在迭代器中不能倒退。您可以使用我的方法,只需反转条件,即检查
lemon
是否在该行中,前进两行,然后检查是否为“下一行”,但如果“下一行”上方同时有三行和两行柠檬,则可能会遗漏柠檬。在这种情况下,@Kasramvd方法会更好。Hi Kasramvd,如果我需要做相反的事情(在某一行的上面数两行,而不是下面),我是否只需要在代码中交换“下一行”和“lemon”?@EmilyT。根据什么样的条件,在某一行上数两行?如果没有条件,只需计算留置权并乘以2。因此,基本上,当你看到“下一行”时,在上面两行的行中计算lemon这个词。@EmilyT。你想数一数单词
lemon
还是包含该单词的行数?这也是你想从最初的问题中得到的吗?我想数一数柠檬这个词,是的,但我没有在我现在意识到的最初问题中具体说明
with open("data.txt") as f:
    lemon_count = 0
    for line in f:
        if "next line" in line:
            next(f) # skip next line
            lemon_count += next(f).count("lemon") # get count for next-next line