#创建由多个(Type,Word)元组构成的列表
tuple = [('direction','north'),('direction','south'),('direction','east'),('verb','go'),('noun','bear')]
list = [x[1] for x in tuple]#创建一个只包含每个元组第二个元素的列表
result = []
def lexicon_scan(sentence):
words_list = sentence.split()#将输入的句子分成含若干个元素的列表
for i in words_list:
if i in list:
get_test = i
for a,i in enumerate(list):#enumerate()获取for循环的次数
if get_test == i:
result.append(tuple[a])
else:
pass
return result
print lexicon_scan("go south north see a bear")
运行结果截图: