python - 用段落中的常用词制作字典
问题描述
请帮忙。我对如何执行此操作感到非常困惑:
这是我目前拥有的:
def similarityAnalysis(paragraph1, paragraph2):
dict = {}
for word in lst:
if word in dict:
dict[word] = dict[word] + 1
else:
dict[word] = 1
for key, vale in dict.items():
print(key, val)
解决方案
见下文。
- 为了找到常用词,我们使用集合交集
- 对于计数,我们使用 dict
代码
lst1 = ['jack','Jim','apple']
lst2 = ['chair','jack','ball','steve']
common = set.intersection(set(lst1),set(lst2))
print('commom words below:')
print(common)
print()
print('counter below:')
counter = dict()
for word in lst1:
if word not in counter:
counter[word] = [0,0]
counter[word][0] += 1
for word in lst2:
if word not in counter:
counter[word] = [0,0]
counter[word][1] += 1
print(counter)
输出
commom words below:
{'jack'}
counter below:
{'jack': [1, 1], 'Jim': [1, 0], 'apple': [1, 0], 'chair': [0, 1], 'ball': [0, 1], 'steve': [0, 1]}
推荐阅读
- ibm-cloud-infrastructure - 获取用于 SoftLayer 帐户中的卷的字节数
- django - 在无法访问服务器的情况下使 JS 中的 Django 会话无效
- pandas - 使用 pip 安装 pandas 时出错:找不到满足要求 numpy==1.9.3 的版本
- c++ - How can I use different #defines for one project used by two different solutions without creating new configurations
- javascript - Puppeteer - 无限滚动情况
- powerbi - DAX 表达式 Power BI 中筛选的 LOD 计算
- ios - UIDropShadowView 在翻转水平模式转换期间显示
- javascript - MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。添加了 11 个消息列表。使用emitter.setMaxListeners() 增加限制
- webpack - 防止在 css 模块中重复解析较少的导入
- docker - 将 docker 映像标记/推送到在 UNIX 域套接字上本地运行的注册表