dict to list, and compare lists python
<p>I have made a function, were I count how many times each word is used in a file, that will say the word frequency. Right now the function can calculate the sum of all words, and show me the seven most common words and how many times they are used. Now I want to compare my first file were I have analyzed the word frequency with another file were I have the most common words used in the english language, and I want to compare those words with the words I have in my first file to see if any of the words matches. </p>

<p>What I have come up to is to make lists of the two files and then compare them with each other. But the code I wrote for this doesn't give me any output, any idea on how I can solve this?</p>

<pre><code>def CountWords():
filename = input('What is the name of the textfile you want to open?: ')
if filename == "alice" or "alice-ch1.txt" or " ":
    file = open("alice-ch1.txt","r")
    print('You want to open alice-ch1.txt')
    wordcount = {}
    for word in file.read().split():
        if word not in wordcount:
            wordcount[word] = 1
        else:
            wordcount[word] += 1                                         
    wordcount = {k.lower(): v for k, v in wordcount.items() }
    print (wordcount)

    sum = 0
    for val in wordcount.values():
        sum += val
    print ('The total amount of words in Alice adventures in wonderland: ' + str(sum))
    sortList = sorted(wordcount.values(), reverse = True)
    most_freq_7 = sortList[0:7]
    #print (most_freq_7)
    print ('Totoro says: The 7 most common words in Alice Adventures in Wonderland:')
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[0])] + " " + str(most_freq_7[0]))
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[1])] + " " + str(most_freq_7[1]))
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[2])] + " " + str(most_freq_7[2]))
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[3])] + " " + str(most_freq_7[3]))
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[4])] + " " + str(most_freq_7[4]))
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[5])] + " " + str(most_freq_7[5]))
    print(list(wordcount.keys())[list(wordcount.values()).index(most_freq_7[6])] + " " + str(most_freq_7[6]))

    file_common = open("common-words.txt", "r")
    commonwords = []
    contents = file_common.readlines()

    for i in range(len(contents)):
        commonwords.append(contents[i].strip('\n'))
    print(commonwords) 

#From here's the code were I need to find out how to compare the lists:
    alice_keys = wordcount.keys()
    result = set(filter(set(alice_keys).__contains__, commonwords))
    newlist = list()


    for elm in alice_keys:
        if elm not in result:
            newlist.append(elm)
    print('Here are the similar words: ' + str(newlist)) #Why doesn't show?


else:
    print ('I am sorry, that filename does not exist. Please try again.')            
</code></pre>

<p>I'm not in front of an interpreter so my code might be slightly off.  But try something more like this.</p>

<pre><code>from collections import Counter
with open("some_file_with_words") as f_file
  counter = Counter(f_file.read())
  top_seven = counter.most_common(7)
  with open("commonwords") as f_common:
    common_words = f_common.read().split()
    for word, count in top_seven:
      if word in common_words:
        print "your word " + word + " is in the most common words!  It appeared " + str(count) + " times!"
</code></pre>

python list dictionary key