5

我是编码领域的新手,我没有受到非常热烈的欢迎。我一直在尝试通过在线教程http://learnpythonthehardway.org/book/学习 python 。在练习 48 和 49 之前,我一直在努力读完这本书。这就是他让学生放松并说“你想办法”的地方。但我根本做不到。我知道我需要创建一个可能单词的词典,并且我需要扫描用户输入以查看它是否与词典中的任何内容匹配,但仅此而已!据我所知,我需要创建一个名为 lexicon 的列表:

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 中,他导入了词典并在 python 内部打印 lexicon.scan("input") 并返回元组列表,例如:

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

'scan()' 是预定义函数还是他在词典模块中创建了该函数?我知道如果你使用'split()',它会创建一个包含输入中所有单词的列表,但是它如何将'go'分配给元组('verb','go')?

我是不是差远了?我知道我问了很多,但我到处搜索了几个小时,但我无法自己解决这个问题。请帮忙!我会永远爱你!

4

7 回答 7

3

根据 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()

他指出,数字和异常的处理方式不同。

于 2014-05-08T02:35:29.110 回答
3

我不会使用列表来制作词典。你正在将单词映射到它们的类型,所以制作一个字典。

这是我可以给出的最大提示,而无需编写整个内容:

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
于 2013-03-15T04:49:21.507 回答
2

最后我做到了!

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
于 2013-03-20T02:04:39.460 回答
2

这是一个非常酷的练习。我不得不研究了几天,终于让它工作了。这里的其他答案没有显示如何实际使用内部包含元组的列表,就像电子书所暗示的那样,所以它会这样做。所有者的回答不太有效,lexicon[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

干杯!

于 2016-01-03T22:56:13.540 回答
1

像这里的大多数人一样,我是编码世界的新手,尽管我在下面附上了我的解决方案,因为它可能会帮助其他学生。

我已经看到了一些我可以实施的更有效的方法。但是,该代码处理了练习的每个用例,并且由于我是根据初学者的想法自己编写的,因此它不需要复杂的快捷方式,并且对于其他初学者来说应该很容易理解。

因此,我认为这可能对其他人的学习有益。让我知道你的想法。干杯!

class Lexicon(object): 

def __init__(self):
    self.sentence = []
    self.dictionary = {
        'north' : ('direction','north'),
        'south' : ('direction','south'),
        'east' : ('direction','east'),
        'west' : ('direction','west'),
        'down' : ('direction','down'),
        'up' : ('direction','up'),
        'left' : ('direction','left'),
        'right' : ('direction','right'),
        'back' : ('direction','back'),
        'go' : ('verb','go'),
        'stop' : ('verb','stop'),
        'kill' : ('verb','kill'),
        'eat' : ('verb', 'eat'),
        'the' : ('stop','the'),
        'in' : ('stop','in'),
        'of' : ('stop','of'),
        'from' : ('stop','from'),
        'at' : ('stop','at'),
        'it' : ('stop','it'),
        'door' : ('noun','door'),
        'bear' : ('noun','bear'),
        'princess' : ('noun','princess'),
        'cabinet' : ('noun','cabinet'),
    }

def scan(self, input):
    loaded_imput = input.split()
    self.sentence.clear()

    for item in loaded_imput:
        try:
            int(item)
            number = ('number', int(item))
            self.sentence.append(number)
        except ValueError:
            word = self.dictionary.get(item.lower(), ('error', item))
            self.sentence.append(word)

    return self.sentence
lexicon = Lexicon()
于 2020-06-02T19:56:40.893 回答
1

显然 Lexicon 是 ex48 文件夹中的另一个 python 文件。

like: ex48
      ----lexicon.py

所以你正在从 ex 48 文件夹中导入 lexicon.py。

scan 是 lexicon.py 中的一个函数

于 2017-10-27T16:30:55.920 回答
-1

这是我的 ex48 扫描词典版本。我也是编程初学者,python是我的第一语言。因此,该程序可能无法达到其目的,但经过多次测试,结果还是不错的。请随时改进代码。

警告

如果你还没有尝试自己做这个练习,我鼓励你尝试而不用看任何例子。

警告

我喜欢编程的一件事是,每次遇到问题时,我都会花很多时间尝试不同的方法来解决问题。我花了几个星期来尝试创建结构,作为一个初学者,我真的学到了很多东西,而不是从别人那里复制,这真的很有意义。

以下是我的词典并在一个文件中搜索。

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

verbs = [('verb', 'go'),
        ('verb', 'stop'),
        ('verb', 'kill'),
        ('verb', 'eat')
]

stop_words = [('stop', 'the'),
            ('stop', 'in'),
            ('stop', 'of'),
            ('stop', 'from'),
            ('stop', 'at'),
            ('stop', 'it')
]

nouns = [('noun', 'door'),
        ('noun', 'bear'),
        ('noun', 'princess'),
        ('noun', 'cabinet')
]   

library = tuple(nouns + stop_words + verbs + direction)

#below is the search method with explanation.

def convert_number(x):
try:
    return int(x)
except ValueError:
    return None


def scan(input):
#include uppercase input for searching. (Study Drills no.3)
lowercase = input.lower()
#element is what i want to search.
element = lowercase.split()
#orielement is the original input which have uppercase, for 'error' type
orielement = input.split()
#library is tuple of the word types from above. You can replace with your data source.
data = library
#i is used to evaluate the position of element
i = 0
#z is used to indicate the position of output, which is the data that match what i search, equals to "i".
z = 0
#create a place to store my output.
output = []
#temp is just a on/off switch. Turn off the switch when i get any match for that particular input.
temp = True
#creating a condition which evaluates the total search needed to be done and follows the sequence by +1.
while not(i == len(element)):
    try:
        #j is used to position the word in the library, eg 'door', 'bear', 'go', etc which exclude the word type.
        j = 0
        while not (j == len(data)):
            #data[j][1] all the single word in library
            matching = data[j][1]
            #when the word match, it will save the match into the output.
            if (matching == element[i]):
                output.append(data[j])
                #print output[z]
                j += 1
                z += 1
                #to switch off the search for else: below and go to next input search. Otherwise they would be considerd 'error'
                temp = False
            #else is everything that is not in the library.
            else:
                while (data[j][1] == data [-1][1]) and (temp == True):
                    #refer to convert_number, to test if the input is a number, here i use orielement which includes uppercase
                    convert = convert_number(orielement[i])
                    #a is used to save number only.
                    a = tuple(['number', convert])
                    #b is to save everything
                    b = tuple(['error', orielement[i]])
                    #conver is number a[1] is the access the number inside, if it returns None from number then it wont append. 
                    if convert == a[1] and not(convert == None):    
                        output.append(a)
                        temp = False
                    else:
                        output.append(b)
                        #keep the switch off to escape the while loop!
                        temp = False
                #searching in next data
                j += 1
        #next word of input
        i += 1
        temp = True
    except ValueError:
        return output
else:
    pass
return output
于 2018-12-24T02:45:00.357 回答