下面的代码是为了找出出现在两个字符串中最多的前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)没有输出。怎么了?你知道吗
很可能是你的压痕。你知道吗
您可以依赖
set
方法。一旦两个列表都有了,就可以将它们转换为集合。公共子集是两个集合的交集,您可以简单地取两个原始集合的差:您发布的代码没有调用函数
new(negativetop150words, positivetop150words)
同样根据Jesse的注释,print(i)命令在函数之外。以下是对我有用的代码:这张照片:
相关问题 更多 >
编程相关推荐