如何计算前150个单词并从2个列表中删除常用单词?

时间:2018-06-08 11:22:36

标签: python python-3.x if-statement counter removeall

以下代码是查找出现在2个字符串中最多的前150个单词。

pwords = re.findall(r'\w+',p)
ptop150words=Counter(pwords).most_common(150)
sorted(ptop150words)

nwords = re.findall(r'\w+',n)
ntop150words=Counter(nwords).most_common(150)
sorted(ntop150words)

以下代码用于删除2个字符串中出现的常用字词。

def new(ntopwords,ptopwords):
    for i in ntopwords[:]:
        if i in potopwords:
            ntopwords.remove(i)
            ptopwords.remove(i)
print(i)

但是,打印(i)没有输出。怎么了?

3 个答案:

答案 0 :(得分:0)

最有可能是你的缩进。

new(negativetop150words,positivetop150words):
    for i in negativetop150words[:]:
        if i in positivetop150words:
            negativetop150words.remove(i)
            positivetop150words.remove(i)
            print(i)

答案 1 :(得分:0)

您发布的代码不会调用函数new(negativetop150words, positivetop150words)同样根据Jesse的注释,print(i)命令在函数外部。这是适用于我的代码:

import re
from collections import Counter

def new(negativetop150words, positivetop150words):
    for i in negativetop150words[:]:
        if i in positivetop150words:
            negativetop150words.remove(i)
            positivetop150words.remove(i)
            print(i)

    return negativetop150words, positivetop150words

positive = 'The FDA is already fairly gung-ho about providing this. It receives about 1,000 applications a year and approves all but 1%. The agency makes sure there is sound science behind the request, and no obvious indication that the medicine would harm the patient.'
negative = 'Thankfully these irritating bits of bureaucracy have been duly dispatched. This victory comes courtesy of campaigning work by a libertarian think-tank, the Goldwater Institute, based in Arizona. It has been pushing right-to-try legislation for around four years, and it can now be found in 40 states. Speaking about the impact of these laws on patients, Arthur Caplan, a professor of bioethics at NYU School of Medicine in New York, says he can think of one person who may have been helped.'

positivewords = re.findall(r'\w+', positive)
positivetop150words = Counter(positivewords).most_common(150)
sorted(positivetop150words)

negativewords = re.findall(r'\w+', negative)
negativetop150words = Counter(negativewords).most_common(150)

words = new(negativewords, positivewords)

打印:

a
the
It
and
about
the

答案 2 :(得分:0)

您可以依赖set方法。一旦有了这两个列表,就可以将它们转换为集合。公共子集是两组的交集,您可以简单地从两个原始集中获取差异:

positiveset = set(positivewords)
negativeset = set(negativewords)
commons = positiveset & negativeset
positivewords = sorted(positiveset - commons)
negativewords = sorted(negativeset - commons)
commonwords = sorted(commons)