kojp
2011-11-23 02:17:46 +08:00
不google,不百度,我终于通过我自己的脑细胞。。敲出了这么一段代码(learn python the hard way 的48课习题)
尽显摆,求批评~~~
def scan(inputstr):
strs=inputstr.split()
direction_list=['north','south','east','west']
verb_list=['go','kill','eat']
noun_list=['bear','princess']
stop_list=['the','in','of']
numbers_list=['1234','91234','3']
errors_list=['ASDFADFASDF','IAS']
sentence=[]
for strn in strs:
if strn in direction_list:
strn_new=('direction',strn)
sentence.append(strn_new)
elif strn in verb_list:
strn_new=('verb',strn)
sentence.append(strn_new)
elif strn in noun_list:
strn_new=('noun',strn)
sentence.append(strn_new)
elif strn in stop_list:
strn_new=('stop',strn)
sentence.append(strn_new)
elif strn in numbers_list:
strn_new=('number',int(strn))
sentence.append(strn_new)
elif strn in errors_list:
strn_new=('error',strn)
sentence.append(strn_new)
else:
sentence.append(strn)
return sentence