2014-01-23 123 views
0

我無法訪問我製作的字典中的某些值。在我的代碼中,我在閱讀文件時做了兩個不同的字典。我的代碼是這樣的:python字典鍵不工作

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'"])]) 

比方說,我有鍵值對{奧克斯:「有」]}。 問題是,如果我=輔助,例如,x只是設置爲一個空列表,當我真的想等於[「有」]。

我不知道我在做什麼/訪問不正確。有任何想法嗎?謝謝!

+0

這將會是可能更容易顯示輸出的結果self.original_grammar.terminal_rules.items(),並且您可能想要爲terminal_rules和non_terminal_rules顯示它。以這種方式查看字典會更容易,而不必單獨查看鍵/值。 – DivineWolfwood

+0

您的輸出中沒有空列表,'['','「]'不被視爲空列表。 –

+0

與鍵:值對'{「Aux」:[「have」]}','i ==「Aux」','nonterminal_rules [i] == [「have」]'和'terminal_rules [j [ 0]]'是'KeyError',這意味着對於'defaultdict(list)'它創建一個空列表並將其分配給'x'。 –

回答

1

我假設從閱讀你的代碼,你想所有的事情開始和結束',正確嗎?在這種情況下,你可能想要

if RHS[0] == "'" and RHS[-1] == "'" : 
    terminal_rules[LHS].append(RHS.strip()) 

由於0是字符串的第一個字符:)。如果'不是分割字符串的第二個字符,那麼現在它會將所有內容添加到non_terminal_rules。

+0

是的,這就是我想象的,但是當我使用RHS [0]而不是RHS [1]時,字典最終變空了,我不知道爲什麼...... – user3229872

0

如果你想設置terminal_rules是每個鍵:在nonterminal_rules值對的長度爲1,這樣做:

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

for line in open(file, 'r').readlines(): 
# Do stuff here as you've done above 

terminal_rules = {key:value for key,value in nonterminal_rules.items() if len(value) == 1} 
+0

不完全。 nonterminal_rules [i]應該產生一個列表清單。如果這個生成的列表只有一個成員,我想查找該列表的第一個索引,將它用作terminal_rules中的鍵,然後保存其關聯的值。我可以查看我的變量窗口,看看terminal_rules何時有一個給定的密鑰,但即使我看到它在那裏,代碼似乎也會產生第二個密鑰並返回一個空列表。 – user3229872

+0

這是一個例子。假設nonterminal_rules有一個條目{AUX:['Aux']},並且在terminal_rules中有條目{Aux:[「is」,「was」]}。我最終想要創建一個新的字典,就像{AUX:[「is」,「was」]}。我需要爲一些條目執行此操作,並且只有當nonterminal_rules中的對的值爲len == 1時(即不適用於{S:[NP,VP]等條目) – user3229872