2013-03-15 24 views
3

我是編程領域的新手,我還沒有收到非常熱烈的歡迎。我一直在嘗試通過在線教程http://learnpythonthehardway.org/book/來學習python。我已經能夠通過這本書奮鬥了,直到練習48 & 49.那就是他讓學生變得鬆散並且說「你知道了」。但我根本不能。我知道我需要創建一個可能詞彙的詞典,並且我需要掃描用戶輸入以查看它是否與詞典中的任何內容相匹配,但這就是它!從我所知道的情況來看,我需要創建一個名爲lexicon的列表:在Python中創建詞典和掃描器

lexicon = [ 
    ('directions', 'north'), 
    ('directions', 'south'), 
    ('directions', 'east'), 
    ('directions', 'west'), 
    ('verbs', 'go'), 
    ('verbs', 'stop'), 
    ('verbs', 'look'), 
    ('verbs', 'give'), 
    ('stops', 'the'), 
    ('stops', 'in'), 
    ('stops', 'of'), 
    ('stops', 'from'), 
    ('stops', 'at') 
] 

是嗎?我不知道接下來要做什麼?我知道列表中的每個項目都稱爲元組,但這對我來說並不意味着什麼。我如何獲取原始輸入並將其分配給元組?你知道我的意思?所以在練習49,他進口的詞彙,只是裏面蟒蛇打印lexicon.scan(「輸入」)等,例如它返回的元組的列表:

from ex48 import lexicon 
>>> print lexicon.scan("go north") 
[('verb', 'go'), ('direction', 'north')] 

是「掃描()」的預定義功能或做他在詞典模塊中創建函數?我知道如果你使用'split()',它會創建一個包含輸入中所有單詞的列表,但它是如何爲元組指定'go'('verb','go')?

我剛剛離開嗎?我知道我問了很多問題,但是我到處搜查了幾個小時,我無法自己找出這個問題。請幫忙!我會永遠愛你!

+0

就像一個建議 - 不要期待來自編碼世界的熱烈歡迎。這是一個菜鳥的錯誤。如果你真的喜歡它,你不會在乎你受到多麼熱烈的歡迎。 – wswld 2013-04-22 07:51:29

回答

2

我不會使用列表來製作詞典。你將單詞映射到他們的類型,所以創建一個字典。

這裏是最大的提示,我可以不用寫了整個事情給:

lexicon = { 
    'north': 'directions', 
    'south': 'directions', 
    'east': 'directions', 
    'west': 'directions', 
    'go': 'verbs', 
    'stop': 'verbs', 
    'look': 'verbs', 
    'give': 'verbs', 
    'the': 'stops', 
    'in': 'stops', 
    'of': 'stops', 
    'from': 'stops', 
    'at': 'stops' 
} 

def scan(sentence): 
    words = sentence.lower().split() 
    pairs = [] 

    # Iterate over `words`, 
    # pull each word and its corresponding type 
    # out of the `lexicon` dictionary and append the tuple 
    # to the `pairs` list 

    return pairs 
+0

如何將它們從字典中提取出來?我嘗試了 lexicon.items() 但是,這隻能給出13對中的3個,它不僅僅是輸入中的3個。 我不知道我在做什麼。對不起。 – Zaqory 2013-03-15 06:04:08

+0

@Zaqory:這在[練習39](http://learnpythonthehardway.org/book/ex39.html)中有介紹。 – Blender 2013-03-15 06:04:56

+0

lexicon.items() 返回整個字典中的元組,但'items'只需要0個參數,所以只得到我想要的元組,我不能只將 lexicon.items('go') [('go','verbs')] 我怎樣才能調用元組? – Zaqory 2013-03-20 00:37:07

1

最後我做到了!

lexicon = { 
    ('directions', 'north'), 
    ('directions', 'south'), 
    ('directions', 'east'), 
    ('directions', 'west'), 
    ('verbs', 'go'), 
    ('verbs', 'stop'), 
    ('verbs', 'look'), 
    ('verbs', 'give'), 
    ('stops', 'the'), 
    ('stops', 'in'), 
    ('stops', 'of'), 
    ('stops', 'from'), 
    ('stops', 'at') 
    } 

def scan(sentence): 

    words = sentence.lower().split() 
    pairs = [] 

    for word in words: 
     word_type = lexicon[word] 
     tupes = (word, word_type) 
     pairs.append(tupes) 

    return pairs 
2

根據ex48指令,您可以爲每種單詞創建幾個列表。這是第一個測試用例的示例。返回的值是一個元組列表,因此您可以爲每個給定的單詞添加該列表。

direction = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back'] 

class Lexicon: 
    def scan(self, sentence): 
     self.sentence = sentence 
     self.words = sentence.split() 
     stuff = [] 
     for word in self.words: 
      if word in direction: 
       stuff.append(('direction', word)) 
     return stuff 

lexicon = Lexicon() 

他注意到數字和例外的處理方式不同。

1

這是一個非常酷的練習。我不得不研究幾天,終於搞定了。這裏的其他答案並沒有說明如何像電子書版本一樣實際使用帶有元組的列表,所以這樣做就可以做到。所有者的答案並不奏效,詞典[word]要求整數而不是str。

lexicon = [('direction', 'north', 'south', 'east', 'west'), 
      ('verb', 'go', 'kill', 'eat'), 
      ('nouns', 'princess', 'bear')] 
def scan(): 
    stuff = raw_input('> ') 
    words = stuff.split() 
    pairs = [] 

    for word in words: 

     if word in lexicon[0]: 
      pairs.append(('direction', word)) 
     elif word in lexicon[1]: 
      pairs.append(('verb', word)) 
     elif word in lexicon[2]: 
      pairs.append(('nouns', word)) 
     else: 
      pairs.append(('error', word)) 

    print pairs 

乾杯!

0

明顯Lexicon是ex48文件夾中的另一個python文件。

like: ex48 
     ----lexicon.py 

所以你從EX 48文件夾導入lexicon.py。

掃描是一個函數裏面lexicon.py