我目前正在通過LPTHW,我達到excercise 48,這是我第一次碰到磚牆。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
因此問題是:以輸入(thewords)後,我怎麼過的元組列表中搜索正確,然後返回特定的元組是的一部分嗎?
在此先感謝任何排序的答案和建議,真的堅持這一個。
想想你需要'方向'來保存你的功能。是否需要三次保持「方向」文字? –
謝謝,看起來好像我只是在爲自己複雜的事情。我只是簡單地將它改爲僅包含第二個元素的列表,並使其工作。 – Dairylee