0

作成した辞書の一部の値にアクセスできません。私のコードでは、ファイルを読みながら 2 つの異なる辞書を作成しました。私が持っているコードはこれです:

nonterminal_rules = defaultdict(list)
terminal_rules = defaultdict(list)

for line in open(file, 'r').readlines():
    LHS,RHS = line.strip().split("->")
    if RHS[1] == "'" and RHS[-1] == "'" :
        terminal_rules[LHS].append(RHS.strip())
    else:
        nonterminal_rules[LHS].append(RHS.split())

for i in nonterminal_rules:
    for j in nonterminal_rules[i]:
        if len(j) == 1:
            x = terminal_rules[j[0]])

私の辞書のキーと値は次のとおりです。

print(self.original_grammar.terminal_rules.items())
dict_items([('NN ', ["'body'", "'case'", "'immunity'", "'malaria'", "'mouse'", "'pathogen'", "'research'", "'researcher'", "'response'", "'sepsis'", "'system'", "'type'", "'vaccine'"]), ('NNS ', ["'cells'", "'fragments'", "'humans'", "'infections'", "'mice'", "'Scientists'"]), ('Prep ', ["'In'", "'with'", "'in'", "'of'", "'by'"]), ('IN ', ["'that'"]), ('Adv ', ["'today'", "'online'"]), ('PRP ', ["'this'", "'them'", "'They'"]), ('Det ', ["'a'", "'A'", "'the'", "'The'"]), ('RP ', ["'down'"]), ('AuxZ ', ["'is'", "'was'"]), ('VBN ', ["'alerted'", "'compromised'", "'made'"]), ('Adj ', ["'dendritic'", "'immune'", "'infected'", "'new'", "'Systemic'", "'weak'", "'whole'", "'live'"]), ('VBN  ', ["'discovered'"]), ('Aux ', ["'have'"]), ('VBD ', ["'alerted'", "'injected'", "'published'", "'rescued'", "'restored'", "'was'"]), ('COM ', ["','"]), ('PUNC ', ["'?'", "'.'"]), ('PossPro ', ["'their'", "'Their'"]), ('MD ', ["'Will'"]), ('Conj ', ["'and'"]), ('VBP ', ["'alert'", "'capture'", "'display'", "'have'", "'overstimulate'"]), ('VB  ', ["'work'"]), ('VBZ ', ["'invades'", "'is'", "'shuts'"]), ('NNP ', ["'Dr'", "'Jose'", "'Villadangos'"])])

キーと値のペア {Aux:["have"]} があるとします。問題は、たとえば、i = Aux の場合、実際には ["have"] と等しくなりたいときに x が空のリストとして設定されることです。

何をしているのか、間違ってアクセスしているのかわかりません。何か案は?ありがとう!

4

2 に答える 2