您的位置:首页技术文章
文章详情页

python - 如何匹配文本每个单词在另一个文本中的单词,及该单词对应的值?

浏览:70日期:2022-06-27 09:17:26

问题描述

文本ttt.txt内容:president said would bill program loan farmers corn committee department agriculture usda house 文本sss.txt内容:Topic 0th:

said 0.045193would 0.028879bill 0.011087program 0.010718loan 0.008395farmers 0.008237corn 0.008078committee 0.007022department 0.006811agriculture 0.006653usda 0.006547house 0.006494president

Topic 1th:

said 0.044315shares 0.031928stock 0.028001company 0.023888group 0.017063offer 0.016408share 0.016268dlrs 0.016034corp 0.015520common 0.013463president 0.000047

如何在sss中匹配ttt中每个单词分别在2个主题下的单词及对应的值?

问题解答

回答1:

# coding: utf8result = {}with open(’ttt.txt’) as f_t, open(’sss.txt’) as f_s: key_set = set(f_t.read().split()) # 将ttt的每个单词存到key集合 topic = ’’ for line in f_s:if line.startswith(’Topic’): # 储存每个Topic topic = line.strip() result[topic] = {}else: line_split = line.split() if len(line_split) < 2:line_split.append(’None’) # 防止没有值的key key, value = line_split if key in key_set: # 如果第一列在key集合内 就收集值result[topic].update({ key: value})print(result)

标签: Python 编程