为了账号安全,请及时绑定邮箱和手机立即绑定

python将两个字典组合成嵌套字典(文本相似性)

python将两个字典组合成嵌套字典(文本相似性)

阿波罗的战车 2021-09-11 16:44:40
我有以下文件:documents = ["Human machine interface for lab abc computer applications",              "A survey of user opinion of computer system response time",              "The EPS user interface management system",              "System and human system engineering testing of EPS",              "Relation of user perceived response time to error measurement",              "The generation of random binary unordered trees",              "The intersection graph of paths in trees",              "Graph minors IV Widths of trees and well quasi ordering",             "Graph minors A survey"]我从中构建了一个词矩阵:wordmatrix = []wordmatrix = [sentences.split(" ") for sentences in documents]随着输出:[['Human', 'machine', 'interface', 'for', 'lab', 'abc', 'computer', 'applications'], ['A', 'survey', 'of', 'user ', '意见', 'of', '计算机', '系统', '响应', '时间'], ['The', 'EPS', 'user', 'interface', 'management', 'system '], ['System', 'and', 'human', 'system', 'engineering', 'testing', 'of', 'EPS'], ['Relation', 'of', 'user', '感知', '响应', '时间', '到', '错误', '测量'], ['The', 'generation', 'of', 'random', 'binary', 'unordered', '树'], ['The', 'intersection', 'graph', 'of', 'paths', 'in', 'trees'], ['Graph', 'minors', 'IV', 'Widths' , 'of', 'trees', 'and', 'well', 'quasi', 'ordering'], ['Graph', 'minors', 'A', 'survey']]接下来,我想创建一个字典,每个文档都有一个键,单词作为键,值表示单词在文档中出现的频率。但我到此为止:初始化字典dic1 = {}dic2 = {}d = {}第一个字典给每个文档一个键:dic1 = dict(enumerate(sentence for sentence in wordmatrix))
查看完整描述

1 回答

?
猛跑小猪

TA贡献1858条经验 获得超8个赞

您不必组合两个 dict,只有当您有 时dic2,您才可以用 构建一个新的 dict dic2。


for line_num, sentence in enumerate(wordmatrix):

    dic1[line_num] = {}

    for word in sentence:

        dic1[line_num][word] = dic2[word]


查看完整回答
反对 回复 2021-09-11
  • 1 回答
  • 0 关注
  • 185 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信