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

2024-09-30 06:27:26 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码是为了找出出现在两个字符串中最多的前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)

下面的代码是删除出现在两个字符串中的常用词。你知道吗

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

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


Tags: 字符串代码inremostcountercommonsorted
3条回答

很可能是你的压痕。你知道吗

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

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

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

您发布的代码没有调用函数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

相关问题 更多 >

    热门问题