我目前正在经历LPTHW而且我已经达到了excercise 48,这是我第一次碰到砖墙。
这是我给出的测试用例的第一部分
from nose.tools import *
from ex48 import lexicon
def test_direction():
assert_equal(lexicon.scan("north"), [('direction', 'north')])
result = lexicon.scan("north south east")
assert_equal(result, [('direction', 'north'),
('direction', 'south'),
('direction', 'east')])
This question has been asked here before,我注意到目前为止我的解决方案与answer provided by robbyt完全相同。但它仍然无效。
def scan(thewords):
directions = [('direction', 'north'), ('direction', 'south'), ('direction', 'east')]
thewords = thewords.split()
sentence = []
for i in thewords:
if i in directions:
sentence.append(('direction', i))
else:
sentence.append(('error', i))
return sentence
所以问题是:在输入(词)之后,如何正确搜索元组列表然后返回它所属的特定元组?
提前感谢任何答案和建议,真的坚持这个。
答案 0 :(得分:3)
受@ Evee的第一个解决方案的启发(谢谢),这是我通过所有测试的解决方案。也许它使用的代码多于第二种解决方案,但它消除了定义的唯一方法之外的循环。
class Lexicon(object):
def __init__(self):
self.mapping = {
'direction': ['north', 'south', 'east', 'west'],
'verb': ['go', 'kill', 'eat'],
'stop': ['the', 'in', 'of'],
'noun': ['door', 'bear', 'princess', 'cabinet']
}
self.mapping_categories = self.mapping.keys()
def scan(self, input):
self.result = []
for word in input.split():
try:
self.result.append(('number', int(word)))
except ValueError:
for category, item in self.mapping.items():
if word.lower() in item:
found_category = category
break
else:
found_category = 'error'
self.result.append((found_category, word))
return self.result
lexicon = Lexicon()
答案 1 :(得分:2)
感谢Thomas K& amp;我设法完成了练习。我现在非常生气,现在看起来很简单......
directions = ['north', 'south', 'east', 'west', 'down', 'up', 'down', 'right']
verbs = ['go', 'stop', 'kill', 'eat']
stops = ['the', 'in', 'at', 'of', 'from', 'at', 'it']
nouns = ['door', 'bear', 'princess', 'cabinet']
def scan(thewords):
thewords = thewords.split()
sentence = []
for i in thewords:
if i in directions:
sentence.append(('direction', i))
elif i in verbs:
sentence.append(('verb', i))
elif i in stops:
sentence.append(('stop', i))
elif i in nouns:
sentence.append(('noun', i))
elif i.isdigit():
sentence.append(('number', convert_number(i)))
else:
sentence.append(('error', i))
return sentence
def convert_number(s):
try:
return int(s)
except ValueError:
return None
答案 2 :(得分:1)
我不知道练习48引入了多少,但我对你的解决方案有一些评论。
首先,对于每个单词列表使用单独的变量,维护此代码有点困难。其次,i
通常仅在从0
计算自然数时用作变量。
考虑:
_LEXICON = dict(
direction = ['north', 'south', 'east', 'west', 'down', 'up', 'down', 'right'],
verb = ['go', 'stop', 'kill', 'eat'],
stop = ['the', 'in', 'at', 'of', 'from', 'at', 'it'],
noun = ['door', 'bear', 'princess', 'cabinet'],
number = ['0','1','2','3','4','5','6','7','8','9'],
)
def scan(words):
result = []
for word in words.split():
found_category = 'error'
for category, category_lexicon in _LEXICON.items():
if word in category_lexicon:
found_category = category
break
result.append((found_category, word))
return result
但我们可以做得更好;寻找列表中的项目很慢。当你想要查找某些内容时,你需要一本字典:
_LEXICON = dict(...)
_LEXICON_INDEX = dict()
for category, words in _LEXICON:
for word in words:
_LEXICON_INDEX[word] = category
def scan(words):
result = []
for word in words.split():
result.append((_LEXICON_INDEX.get(word, 'error'), word))
return result
当然,这实际上并未通过练习中的所有测试。我会留给你修改我的代码。 ;)
答案 3 :(得分:0)
据推测,你的“指示”列表最终将包含除“方向”之外的其他元素作为第一个条目(继@Thomas K的评论之后)。
如果列出元组的第二个元素:
valid_words = [x[1] for x in directions]
然后修改您的代码:
for i in thewords:
if i in valid_words:
sentence.append(directions[valid_words.index(i)])
会给你相关的元组。