I have 2 different dictionaries (book1words and book2words) and words in it. Some words are the same in both dictionaries and some are not. I have to find the common words in these dictionaries and print them in the number of words the USER WANTS to see. However, I have to specify the number of times the common word occurs in 1st dictionary and 2nd dictionary, and lastly, how many times it occurs in total.It must be from most repetitive to least repetitive. We can not use any special library for python like collections or counter.Output should be like this:
Common word frequent1 frequent2 frequentsum
1.print 20 19 39
2.number 10 8 18
3.program 5 7 12
Here is what I've tried:
book1words_book1 = {}
for word in replacingpunctuations_book1:
if word not in book1words_book1:
book1words_book1[word] = 1
else:
book1words_book1[word] += 1
book2words = {}
for word in replacingpunctuations_book2:
if word not in book2words:
book2words[word] = 1
else:
book2words[word] += 1
commonwords={}
for word in book1words_book1:
if word in book2words:
commonwords[word]=1
else:
commonwords[word] +=1
commonwords = sorted(commonwords.items(), key=lambda x: x[1], reverse=True)
z = int(input("How many words that you want to see?"))
print(" WORDCOMMON FREQUENCY")
for k, v in commonwords[:z]:
print(k, v)