1

In continuation to my previous question: Comparison of the result of iterating a line slice with a list from a file

To optimize the code, I wrote a function. It checks whether the elements of the list of their .txt file match the specified text and creates a list of matches (hidden_list). Next, it generates additional lists from the elements of the list of matches (hidden_list) with the condition that an element with a certain length will be removed from the list.

rus_words = open('russian.txt') #opening a file in read mode

text = 'АРВТРВТПЛЯЖАОВРКОНСТРУКТОР' #Initial line

len_of_words = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
        
def search_in(list_rus, some_text, len_of_word):
    
    hidden_words = []
    
    for line in list_rus:
        if line.strip() in some_text.lower():
            hidden_words.append(line.strip())
    
    for i in hidden_words:
        if len(i) >= len_of_word:
            hidden_words.remove(i)
    
    return hidden_words
    

for x in len_of_words:
    results = search_in(rus_words, text, x)
    print(f'The word length is longer: {x}, number of words = {len(results)}')
    print(results)

As a result, it returns a list to me at x = 0, where the original hidden_words list is actually located, with other values of x (1,2...12), an empty list is returned. I can't understand why it returns empty lists

SasambaDio
  • 23
  • 6
  • 1
    What's your question? – Stuart May 17 '23 at 10:58
  • 1
    Not sure if this is related to your question/problem but check [How to remove items from a list while iterating?](https://stackoverflow.com/q/1207406/4046632). There is definitely a problem in that part of your code `hidden_words.remove(i)` – buran May 17 '23 at 11:01
  • 3
    `rus_words` is a file object, so once you've done `for line in rus_words` once, doing it again will not yield any more lines. See: https://stackoverflow.com/questions/10255273/iterating-on-a-file-doesnt-work-the-second-time – slothrop May 17 '23 at 11:05
  • Regarding `remove`, it might be better not to use it. Instead, build the list of all hidden words once, and do something like `filter(lambda i: len(i) < len_of_word, hidden_words)` to get a version filtered by max word length. – slothrop May 17 '23 at 11:12

0 Answers0