0

I have two dictionary. Each of dictionary include words. some words are common some are not. I want to show to output common word frequency1 frequency2 and frequency sum. How can I do that ? and I have to find the top 20.

For example my output must be like:

Common WORD frequ1. freq2 freqsum
1 print      10.     5.      15
2 number.     2.     1.       3. 
3 program     19.    20.      39

Here is my code:

commonwordsbook1andbook2 = []
for element in finallist1:
    if element in finallist2:
        commonwordsbook1andbook2.append(element)

common1 = {}
for word in commonwordsbook1andbook2:
    if word not in common1:
        common1[word] = 1
    else:
        common1[word] += 1
        
common1 = sorted(common1.items(), key=lambda x: x[1], reverse=True) #distinct2

for k, v in wordcount2[:a]:
    print(k, v)  

2 Answers2

0

Assuming that the dictionaries have individual frequencies of each word, we can do something simpler. Like...

print("Common Word | Freq-1 | Freq-2 | Freq-Sum")
for i in freq1:
   if i in freq2:
      print(i,freq1[i],freq2[i],freq1[i]+freq2[i])
-1

Since you aren't allowed to use Counter, you can implement the same functionality using dictionaries. Let's define a function to return a dictionary that contains the counts of all words in the given list. Dictionaries have a get() function that gets the value of the given key, while also allowing you to specify a default if the key is not found.

def countwords(lst):
    dct = {}
    for word in lst:
        dct[word] = dct.get(word, 0) + 1
    return dct


count1 = countwords(finallist1)
count2 = countwords(finallist2)

words1 = set(count1.keys())
words2 = set(count2.keys())

count1.keys() will give us all the unique words in finallist1. Then we convert both of these to sets and then find their intersection to get the common words.

common_words = words1.intersection(words2)

Now that you know the common words, printing them and their counts should be trivial:

for w in common_words:
    print(f"{w}\t{count1[w]}\t{count2[w]}\t{count1[w] + count2[w]}")
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70