How can I print the line index of a specific word in a text file?

1.7k views Asked by At

I was trying to find a way to print the biggest word from a txt file, it's size and it's line index. I managed to get the first two done but can't quite figure it out how to print the line index. Can anyone help me?

def BiggestWord():
   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))
   biggest_word = str(max(list_words, key=len))

   print biggest_word
   print len(biggest_words)

   FindWord(biggest_word)

def FindWord(biggest_word):
4

There are 4 answers

1
Inna Zhurba On BEST ANSWER

You don't need to do another loop through your list of largest words from each line. Every for-loop increases function time and complexity, and it's better to avoid unnecessary ones when possible.

As one of the options, you can use Python's built-in function enumerate to get an index for each line from the list of lines, and instead of adding each line maximum to the list, you can compare it to the current max word.

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.: This function doesn't suggest what to do with the line maximum words of the same length, and which of them should be chosen as absolute max. You would need to adjust the code accordingly.

P.P.S.: This example is in Python 3, so change the snippet to work in Python 2.7 if needed.

0
kev1n On

With a limited amount of info I'm working with, this is the best solution I could think of. Assuming that each line is separated by a new line, such as '\n', you could do:

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
0
Gleison On

You can use enumerate in your for to get the current line and sorted with a lambda to get the longest word:

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
0
Yuri Khristich On

Are you aware that there can be:

  • many 'largest' words with the same length
  • several lines contain word(s) with the biggest length

Here is the code that finds ONE largest word and returns a LIST of numbers of lines that contain the word:

# 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)

If you want to get all lines that have words with the same biggest length you need to modify the last two lines in my code this way:

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

print(lines)