My python knowledge is limited to the basics so expect bugs or that something will not work as expected.
Try this:
import fileinput
line_list = []
line_count = 0
for line in fileinput.input(["fin.txt"]):
letter_list = []
for letter in line.lower():
if letter.isalpha():
letter_list.append(letter)
if len(letter_list) != 0:
words = []
for x in range(0, len(letter_list), 6):
word = letter_list[x: x + 6]
words.append(word)
line_list.append(words)
line_count += 1
groups = ["ab", "cd", "ef", "gh", "ij", "kl"]
gr = {}
for la in line_list:
for word in la:
word.sort()
counter = ""
for group in groups:
count = word.count(group[0]) + word.count(group[1])
if count != 0:
counter += str(count)
gr["".join(word)] = "".join(sorted(counter, reverse=True))
for word, count in gr.items():
print(word, count)
print('-'* 30)
print(f"Line count = {line_count}")
Or this:
with open("fin.txt") as file:
line_list = []
line_count = 0
for line in file:
letter_list = []
for letter in line.lower():
if letter.isalpha():
letter_list.append(letter)
if len(letter_list) != 0:
words = []
for x in range(0, len(letter_list), 6):
word = letter_list[x: x + 6]
words.append(word)
line_list.append(words)
line_count += 1
groups = ["ab", "cd", "ef", "gh", "ij", "kl"]
gr = {}
for la in line_list:
for word in la:
word.sort()
counter = ""
for group in groups:
count = word.count(group[0]) + word.count(group[1])
if count != 0:
counter += str(count)
gr["".join(word)] = "".join(sorted(counter, reverse=True))
for word, count in gr.items():
print(word, count)
print('-'* 30)
print(f"Line count = {line_count}")
It should works better for large files. Check if 'Line_count' is correct. If it is then it must be other problem