for word in words 只循环一次的问题

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')

我就想问一下  为何同样的代码得不到同样的结果  我这边出了什么问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值