【问题标题】:dict to list, and compare lists pythondict列出,并比较列表python
【发布时间】:2016-10-14 12:31:04
【问题描述】:

我做了一个函数,如果我计算每个单词在文件中使用了多少次,那就是单词频率。现在该函数可以计算所有单词的总和,并显示七个最常用的单词以及它们的使用次数。现在我想比较我的第一个文件,如果我已经用另一个文件分析了词频,我有英语中最常用的词,我想将这些词与我在第一个文件中的词进行比较,看看是否任何单词匹配。

我想出的是列出这两个文件,然后将它们相互比较。但是我为此编写的代码没有给我任何输出,我知道如何解决这个问题吗?

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.')            

【问题讨论】:

    标签: python list dictionary key


    【解决方案1】:

    我没有在解释器面前,所以我的代码可能有点不对劲。但请尝试更多类似的方法。

    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!"
    

    【讨论】:

    • 感谢@bravosierra99!
    • 它以字符形式出现,尽管“你的词 e 是最常用的词......”而不是词......
    • 您的常用词文件是如何设置的?我有它使用 .split() 这意味着单词需要用空格分隔。您必须根据常用词文件的设置方式调整此代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 2015-02-21
    相关资源
    最近更新 更多