2016-06-21 11 views
-1

OKが私のコードです:pythonの辞書の繰り返しの使用エラー

# create variables 
direction = ('north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back') 
verb = ('go', 'stop', 'kill', 'eat') 
stop = ('the', 'in', 'of', 'from', 'at', 'it') 
noun = ('door', 'bear', 'princess', 'cabinet') 
number = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') 

def scan(word, *words): # '*words' lets you give a variable number of  arguments to a function 
    words = word.split() 
    src = { direction : 'direction', 
      verb : 'verb', 
      stop : 'stop', 
      noun : 'noun', 
      number : 'number' 
      } 
    for k, v in src.items(): 
     if words in k: 
      m = src.get(k) 

    print [(m, w) for w in words] 

scan("I was going north and south") 

問題:

  • 私が1かどうかを確認するためにsrcを通じて言葉を実行した後variable mdic(src)の値を代入しようとしていますwordsの内容は、srcに見つけることができます。

マイエラー:

Traceback (most recent call last): 
    File "koko2.py", line 26, in <module> 
    scan("I was going north and south") 
    File "koko2.py", line 24, in scan 
    print [(m, w) for w in words if w in direction or verb or stop or noun or number] 
UnboundLocalError: local variable 'm' referenced before assignment 
+1

'for'ループは、' k in words in k 'があれば 'm' ** only **を定義します。 'for'の中に' print'を追加して、何が起きているのか、それがあなたの期待に合っているかを見てみましょう。 – Bakuriu

+0

「動詞または停止または名詞または番号」とは何としますか? –

+0

Padraic実際にコード内に重複しているプロセスがあるので、私はそれを削除しました。 –

答えて

1

あなたのロジックは言葉がリストであり、それはm定義を取得することはありませんので、リストには、言葉のあなたのタプルになることができませんでした、すべて間違っています。個別のワードがキー/タプルに含まれていたかどうかを確認する必要があります。 、

('north', 'direction') 
('south', 'direction') 

をまた、あなたは*引数を使用して単語や語句を渡したい場合は使用を:

def scan(s): # '*words' lets you give a variable number of  arguments to a function 
    words = s.lower().split() 
    src = {frozenset(direction): 'direction', 
     frozenset(verb): 'verb', 
     frozenset(stop): 'stop', 
     frozenset(noun): 'noun', 
     frozenset(number): 'number' 
     } 
    for word in words: 
     for k, v in src.items(): 
      if word in k: 
       yield word, v 
       break 


for pair in scan("I was going north and south"): 
    print(pair) 

あなたを与えるだろう:私はO(1)検索のための代わりにキーとしての言葉ののfrozensetを使用します*言葉のみと直接反復:

def scan(*words): # '*words' lets you give a variable number of  arguments to a function 
    src = {frozenset(direction): 'direction', 
      frozenset(verb): 'verb', 
      frozenset(stop): 'stop', 
      frozenset(noun): 'noun', 
      frozenset(number): 'number' 
      } 
    for word in words: 
     for k, v in src.items(): 
      if word in k: 
       yield word, v  
       break 



for pair in scan("I", "was", "going", "north", "and", "south"): 
    print(pair) 
+1

ありがとうパドレイク、私は感謝しています。あなたのコードの中の 'for words in words:'はそれでした。ありがとう。 –

0

if words in k:は、したがって、エラーのでmが定義されることはありません本当のことはありません。あなたはそのようなこと行うことができます

m = "" 
for k, v in src.items(): 
    if words in k: 
     m = src.get(k) 

if m: 
    print([(m, w) for w in words if w in direction or verb or stop or noun or number]) 
0

をあなたのデザインは、おそらく開始するのがベストではないが、ここに私の提案は、次のとおりです

# frozenset are a better option for these static variables...            
direction = ('north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back') 
verb = ('go', 'stop', 'kill', 'eat') 
stop = ('the', 'in', 'of', 'from', 'at', 'it') 
noun = ('door', 'bear', 'princess', 'cabinet') 
number = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') 

# Singles string as key are prefered (just my opinion)              
src = {'direction': direction, 
     'verb': verb, 
     'stop': stop, 
     'noun': noun, 
     'number': number} 


def scan(word, *words): 
    words = word.split() if not words else words 
    for word in words: 
     for k, v in src.items(): 
      if word in v: 
       print((word, k)) 


def scan_with_ret(word, *words): 
    words = word.split() if not words else words 
    ret = [] 
    for word in words: 
     for k, v in src.items(): 
      if word in v: 
       ret.append((word, k)) 
    return ret 


def scan_with_ret_oneliner(word, *words): 
    words = word.split() if not words else words 
    return [(word, k) for word in words for k, v in src.items() if word in v] 


print("scan") 
scan("I was going north and south") 

print("scan_with_ret") 
print(scan_with_ret("I was going north and south")) 

print("scan_with_ret_oneliner") 
print(scan_with_ret_oneliner("I was going north and south")) 

結果:

scan ('north', 'direction') ('south', 'direction') 
scan_with_ret [('north', 'direction'), ('south', 'direction')] 
scan_with_ret_oneliner [('north', 'direction'), ('south', 'direction')] 

・ホープここ

@Padraicカニンガム参考にする:Oups、私はあなたが発電機に正しい答えを見ていません。あいまいな説明のおかげで、彼らはまた、重複してデータを保存します。

+0

nice。感謝の気持ちで、私はPyコードを理解しました。 –

関連する問題