class Lexicon(object):
def _init_(self):
self.directions=('south','north','west','east')
self.verbs=('go','kill','eat','run',
'tell','shoot','sing','love')
self.stops=('the','in','of','to','via')
self.nouns=('bear','princess','MissHei','tiger','dragon')
self.sen=[]
def scan(self,sentence):
self.sentence = sentence
print self.sentence
sentence=self.sentence.split(' ')
print sentence
self.sen=[]
for word in sentence:
print word
'''
if word in self.directions:
self.sen.append(('direction',word))
elif word in self.verbs:
self.sen.append(('verb',word))
elif word in self.stops:
self.sen.append(('stop',word))
elif word in self.nouns:
self.sen.append(('noun',word))
else:
try:
self.sen.append(('number',int(word)))
except ValueError:
self.sen.append(('error',word))
'''
return self.sen
lexicon = Lexicon()
lexicon._init_()
print lexicon.scan("go north to kill the bear")
sentence="go north to kill the bear"
sentence=sentence.split(' ')
print sentence
for word in sentence:
print word
sentences=['a','b','c']
print sentences
for word in sentences:
print word
在类里面的for word in sentence 第二次循环输出的是[ ] 我用外面用同样的方式测试的结果就是对的
另外 我试过在类里面 split(' ')换成split(',')就能得到想要的结果 但是sentence就会变成'go to kill the bear'而不是
('go','to','kill','the','bear')
我就想问一下 为何同样的代码得不到同样的结果 我这边出了什么问题