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