Read the file a line at a time instead of reading the whole thing.
You can use collections.Counter()
to get the counts of each word, then go through this to find all the ones that were repeated at least n
times.
import collections
word_counts = collections.Counter()
with open('task.txt') as f:
for line in f:
word_counts.update(line.lower().split())
n = int(input("enter number :"))
print("the words which are repeated given number of times : ");
words = [word for word, count in word_counts.items() if count >= n]
if len(words) == 0:
print('no word found')
else:
print(*words, sep = '
')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…