
Niranjan S. answered 04/01/19
Tutor
New to Wyzant
Experienced Tutor Specializing in Python
To making it simple to understand I will only do three files. I assume file text are in 1 line. I also assume you want to compare and check which are common.
import itertools
# reading files.
file1 = open('file1','r').read()
file2 = open('file2','r').read()
file3 = open('file3','r').read()
# def to create a file txt to list.
def splitter(file):
f = []
for x in file.split(' '):
f.append(x)
return f
# getting the list from splitter func.
f1 = splitter(file1)
f2 = splitter(file2)
f3 = splitter(file3)
# get the only common.
for x,y,z in itertools.product(f1,f2,f3):
if x == y == z:
result = x
# creating a empty list.
list = {}
# def to get number of times the word contain.
def counterNum(lst):
for x in lst:
if x in result:
list[x] = list.get(x,0)+1
# just running the counter
counterNum(f1)
counterNum(f2)
counterNum(f3)
keys = list(list.keys())
output = open('output.txt','a+')
for x in keys:
output.write(x + ':' + str(list[x]) + '\n')
# done.