Heim  >  Fragen und Antworten  >  Hauptteil

Python – Wie kann man jedes Wort eines Textes einem Wort in einem anderen Text und dem diesem Wort entsprechenden Wert zuordnen?

Text ttt.txt-Inhalt:
Präsident sagte, er würde den Landwirten ein Darlehensprogramm in Rechnung stellen
Maisausschuss der Landwirtschaftsabteilung
USDA-Haus
Text sss.txt-Inhalt:
Thema 0:

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

Thema 1:

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

Wie werden die Wörter und die entsprechenden Werte jedes Wortes in ttt unter zwei Themen in sss abgeglichen?

巴扎黑巴扎黑2667 Tage vor1046

Antworte allen(1)Ich werde antworten

  • 我想大声告诉你

    我想大声告诉你2017-06-30 09:56:38

    
    # coding: utf8
    
    result = {}
    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)
    

    Antwort
    0
  • StornierenAntwort