如何在 python 中打印文本文件中单词的行号?

How can I print the line number of a word in text file in python?

我试图找到一种方法来打印 txt 文件中最大的单词、它的大小以及它在哪一行。我设法完成了另外两个,但不太清楚如何打印行号。谁能帮帮我?

def LargestWord():
   list_words = []
   with open('song.txt', 'r') as infile:
       lines = infile.read().split()
       for i in lines:
          words = i.split()
          list_words.append(max(words, key=len))
   largest_word = str(max(list_words, key=len))

   print largest_word
   print len(largest_words)

   FindWord(largest_word)

def FindWord(largest_word):

在我使用的信息量有限的情况下,这是我能想到的最佳解决方案。假设每一行都由一个新行分隔,例如'\n',你可以这样做:

def FindWord(largest_word):
   with open('song.txt', 'r') as infile:
      lines = infile.read().splitlines()

   linecounter = 1
   for i in lines:
      if largest_word in lines:
         return linecounter
      linecounter += 1

您可以在 for 中使用 enumerate 来获取当前行,并使用 lambda[=16] sorted =] 得到最长的单词:

def longest_word_from_file(filename):
    list_words = []
    with open(filename, 'r') as input_file:
        for index, line in enumerate(input_file):
            words = line.split()
            list_words.append((max(words, key=len), index))
    
    sorted_words = sorted(list_words, key=lambda x: -len(x[0]))
    longest_word, line_index = sorted_words[0]

    return longest_word, line_index

您无需再循环遍历每一行的最大单词列表。每个 for-loop 都会增加函数时间和复杂性,最好尽可能避免不必要的。

作为选项之一,您可以使用 Python 的 built-in 函数 enumerate 从行列表中获取每一行的索引,而不是添加每一行line maximum到列表中,你可以将它与当前的最大单词进行比较。

def get_largest_word():
    # Setting initial variable values
    current_max_word = ''
    current_max_word_length = 0
    current_max_word_line = None

    with open('song.txt', 'r') as infile:
        lines = infile.read().splitlines()
        for line_index, line in enumerate(lines):
            words = line.split()
            max_word_in_line = max(words, key=len)
            max_word_in_line_length = len(max_word_in_line)
            if max_word_in_line_length > current_max_word_length:
                # updating the largest word value with a new maximum word
                current_max_word = max_word_in_line
                current_max_word_length = max_word_in_line_length
                current_max_word_line = line_index + 1  # line number starting from 1

    print(current_max_word)
    print(current_max_word_length)
    print(current_max_word_line)
    return current_max_word, current_max_word_length, current_max_word_line

P.S.: 这个函数不建议如何处理相同长度的行最大单词,以及应该选择其中哪些作为绝对最大。您需要相应地调整代码。

P.P.S.: 此示例在 Python 3 中,因此如果需要,请将代码片段更改为在 Python 2.7 中工作。

你知道可以有:

  • 许多 'largest' 个长度相同的单词
  • 几行包含最大长度的单词

这是查找一个最大单词的代码和 returns 包含该单词的行数列表:

# built a dictionary: 
#   line_num: largest_word_in_this_line
#   line_num: largest_word_in_this_line
#   etc...
# !!! actually, a line can contain several largest words
list_words = {} 
with open('song.txt', 'r') as infile:
    for i, line in enumerate(infile.read().splitlines()):
        list_words[i] = max(line.split(), key=len)

# get the largest word from values of the dictionary
# !!! there can be several different 'largest' words with the same length
largest_word = max(list_words.values(), key=len) 

# get a list of numbers of lines (keys of the dictionary) that contain the largest word
lines = list(filter(lambda key: list_words[key] == largest_word, list_words))

print(lines)

如果你想得到所有包含相同最大长度单词的行,你需要这样修改我代码中的最后两行:

lines = list(filter(lambda key: len(list_words[key]) == len(largest_word), list_words))

print(lines)