码迷,mamicode.com
首页 > 其他好文 > 详细

更复杂的用户输入—扫描单词

时间:2016-08-14 11:44:06      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:

惭愧,惭愧,断断续续地学习,致《笨方法学Python》才学到这里

参考: 习题—48

 

把书里提供的各类型单词弄成一个个列表元素,然后再按类型分成对应的元组

技术分享
 1 # coding: utf-8
 2 
 3 # 方向类的单词、动词、修饰词、名词
 4 directions = "north south east west down up left right back"
 5 verbs = "go stop kill eat"
 6 stops = "the in of from at it"
 7 nouns = "door bear princess cabinet"
 8 ept = []
 9 
10 # 获得各单词类型的元组列表
11 def GetList(variable):
12     
13     # 把字符串按空格分成一个个列表元素
14     variable = variable.split()
15     
16     for i in variable:
17         if "up" in variable:    # 方向类
18             type = ("direction", i)
19         elif "go" in variable:  # 动词类
20             type = ("verbs", i)
21         elif "in" in variable:  # 修饰类
22             type = ("stops", i)
23         elif "door" in variable:# 名词类
24             type = ("nouns", i)
25         
26         # 把各元组添加进空列表
27         ept.append(type)
28             
29 GetList(directions)
30 GetList(verbs)
31 GetList(stops)
32 GetList(nouns)
单词

当然,也可以和苦逼的我一样,直接写成元组列表的形式

技术分享
 1 direction = [(direction, "north"), (direction, south), (direction, east), 
 2              (direction, west), (direction, down), (direction, up), 
 3              (direction, left), (direction, right), (direction, back)]
 4 nouns = [(noun, door), (noun, bear), (noun, princess), (noun, cabinet)]
 5 stops = [(stop, the), (stop, in), (stop, of), (stop, from), (stop, at), (stop, it)]
 6 verbs = [(verb, go), (verb, stop), (verb, kill), (verb, eat)]
 7 
 8 word_list = []
 9 
10 word_list.append(direction)
11 word_list.append(nouns)
12 word_list.append(stops)
13 word_list.append(verbs)
单词

 

更复杂的用户输入—扫描单词

标签:

原文地址:http://www.cnblogs.com/Ruby517/p/5769691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!