【问题标题】:Find the lines in the text file查找文本文件中的行
【发布时间】:2015-11-14 20:46:24
【问题描述】:

您将实现函数 index(),它将文本文件的名称和单词列表作为输入。对于列表中的每个单词,您的函数将在文本文件中查找该单词出现的行并打印相应的行号(编号从 1 开始)。您应该只打开和读取一次文件。

我只能统计一次。

def index(filename, words):
    infile = open(filename)
    content = infile.readlines()
    infile.close()
    count = {}
    for word in words:
        if word in count:
            count[word] += 1
        else:
            count[word] = 1
        for word in count:
            print('{:12}{},'.format(word, count[word]))

    Output :index('raven.txt',['raven'])
        raven       1,
    Desired Output : index('raven.txt',['raven'])
       raven 44, 53, 55, 64, 78, 97, 104, 111, 118, 12(No of lines it appear)

【问题讨论】:

  • 你在第二个for循环中重新定义word ...
  • 不,我不会实现函数index()。你不能告诉我该怎么做。

标签: python list line


【解决方案1】:

可能会遇到同样的问题..我写了下面..

def index(filename, lst):
    infile = open(filename, 'r')
    content = infile.readlines()
    infile.close()

    for word in lst:
        print(word, end = '\t')
        for line in range(len(content)):
            if word in content[line]:
            print(line+1, end= ' ')
        print()

index('raven.txt', ['raven'])

【讨论】:

    【解决方案2】:

    未经测试,但应该可以工作

    def index(filename, words):
        with open(filename, "r") as f:
            for i, line in enumerate(f):
                for word in words:
                    if word in line:
                        return "%s at line %i" % (word, i + 1)
    
    print index("some_filename", ["word1", "word2"])
    

    或者避免嵌套for循环:

    def index(filename, words):
        with open(filename, "r") as f:
            for line, word in itertools.product(enumerate(f), words):
                if word in line[1]:
                    return "%s at line %i" % (word, line[0] + 1)
    
    print index("some_filename", ["word1", "word2"])
    

    并使用列表理解:

    def index(filename, words):
        with open(filename, "r") as f:
            return "\n".join("%s at line %i" % (word, line[0] + 1) for line, word in itertools.product(enumerate(f), words) if word in line[1])
    
    print index("some_filename", ["word1", "word2"])
    

    【讨论】:

    • 感谢您的回复,但我没有得到任何输出。
    • 它只是一个函数......也许你应该调用它?而且我认为您应该自己考虑一下...
    • for 循环中的 for 循环不是很整洁。
    • 你有什么更好的建议?我可以使用列表理解,但我认为它对打印没有用......
    【解决方案3】:

    这个例子怎么样:

    文件1.txt

    Y
    x
    u
    d
    x
    q
    

    代码:

    word='x'
    i = 0
    with open('file1.txt', 'r') as file:
        for line in file:
            i = i +1
            if word in line:
                print(i)
                print('Found It')
    

    在本例中,您读入一个文件,并逐行查看。在每一行中,如果出现一个单词,您就会查看。如果是这种情况,我们会在屏幕上打印一些东西。

    编辑:
    或者在定义中是:

    filename='file1.txt'
    word='x'
    def index(filename, word):
        i = 0
        with open(filename, 'r') as file:
            for line in file:
                i = i +1
                if word in line:
                    print(i)
                    print('Found It')
    
    index(filename, word)
    

    【讨论】:

    • 可以使用enumerate()获取行号。我认为它更pythonic。您的代码没有回答问题。
    • 我的代码确实回答了这个问题,我引用:'在文本文件中找到单词出现的行并打印相应的行号'。
    • 是的,但是函数应该将单词列表作为输入
    猜你喜欢
    • 1970-01-01
    • 2017-02-28
    • 2011-01-09
    • 2020-03-03
    • 2018-03-28
    • 1970-01-01
    • 2013-06-08
    • 2023-02-02
    • 2011-07-06
    相关资源
    最近更新 更多