久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python - 如何匹配文本每個(gè)單詞在另一個(gè)文本中的單詞,及該單詞對(duì)應(yīng)的值?

瀏覽:119日期:2022-06-27 09:17:26

問(wèn)題描述

文本ttt.txt內(nèi)容:president said would bill program loan farmers corn committee department agriculture usda house 文本sss.txt內(nèi)容: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中每個(gè)單詞分別在2個(gè)主題下的單詞及對(duì)應(yīng)的值?

問(wèn)題解答

回答1:

# coding: utf8result = {}with open(’ttt.txt’) as f_t, open(’sss.txt’) as f_s: key_set = set(f_t.read().split()) # 將ttt的每個(gè)單詞存到key集合 topic = ’’ for line in f_s:if line.startswith(’Topic’): # 儲(chǔ)存每個(gè)Topic topic = line.strip() result[topic] = {}else: line_split = line.split() if len(line_split) < 2:line_split.append(’None’) # 防止沒(méi)有值的key key, value = line_split if key in key_set: # 如果第一列在key集合內(nèi) 就收集值result[topic].update({ key: value})print(result)

標(biāo)簽: Python 編程
主站蜘蛛池模板: 濮阳县| 凌源市| 富顺县| 冕宁县| 库伦旗| 绍兴市| 阳城县| 商河县| 额尔古纳市| 宁德市| 毕节市| 大连市| 通城县| 桐庐县| 杭锦旗| 嘉荫县| 黄石市| 宣威市| 盐山县| 本溪市| 霍州市| 鄱阳县| 马鞍山市| 通海县| 民乐县| 阳江市| 竹山县| 宁德市| 辽源市| 万安县| 云霄县| 弋阳县| 介休市| 大邑县| 江西省| 烟台市| 兴隆县| 南充市| 扶余县| 四会市| 吉安县|