python字典练习
#!/bin/python3.4
# coding=utf-8 class lexicon(object):
def __init__(self):
print "define a clase instance!" def scan(self, elements):
# self.elements = raw_input()
directionval = ('north', 'south', 'east', 'west')
verbval = ('go', 'stop', 'kill', 'eat')
nounval = ('door', 'bear', 'princess', 'cabinet')
numval = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
wordtypelist = {'direction':directionval, 'verb':verbval, 'noun':nounval, 'num':numval}
element = elements.split()
result = []
res = ()
for i in range(len(elements.split())):
for key, element[i] in wordtypelist.items():
if element[i] in wordtypelist['direction']:
print("direction element: %s" %(key, element[i]))
elif element[i] in wordtypelist['verb']:
print("verb element: %s" %(key, element))
elif element[i] in wordtypelist['noun']:
print("type: %s element: %s" %(key, element[i]))
res = ('direction', element[i])
result.append(res)
elif element[i] in wordtypelist['num']:
print("type: %s element: %s" %(key, element[i]))
else:
print("element %s is not in dict!!") %(element[i])
res = (key, element[i])
result.append(res)
return result if __name__ == '__main__':
print("##### Start #####")
sentence = raw_input(">> ")
print("type: %s content: %s", type(sentence), sentence)
stuff = lexicon()
stuff.scan(sentence)
print("##### End #####")
python字典练习的更多相关文章
- Python字典和集合
Python字典操作与遍历: 1.http://www.cnblogs.com/rubylouvre/archive/2011/06/19/2084739.html 2.http://5iqiong. ...
- python 字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- python字典中的元素类型
python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...
- python字典copy()方法
python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...
- python 字典实现类似c的switch case
#python 字典实现类似c的switch def print_hi(): print('hi') def print_hello(): print('hello') def print_goodb ...
- python字典的常用操作方法
Python字典是另一种可变容器模型(无序),且可存储任意类型对象,如字符串.数字.元组等其他容器模型.本文章主要介绍Python中字典(Dict)的详解操作方法,包含创建.访问.删除.其它操作等,需 ...
- Python 字典(Dictionary)操作详解
Python 字典(Dictionary)的详细操作方法. Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字 ...
- Python 字典(Dictionary) get()方法
描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
- Python 字典(Dictionary) setdefault()方法
描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...
- python 字典内置方法get应用
python字典内置方法get应用,如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这 ...
随机推荐
- linux操作系统3 vi编辑器
知识内容: 1.Linux基础命令复习 2.vi模式和基本操作 3.vi常用快捷键 一.Linux基础命令复习 目录(文件夹)操作 cd 进入目录 (tab自动补全) pwd 查看当前目录 mkdir ...
- Spring boot Thymeleaf 配置
第一步:pom.xml加入依赖 <!-- HTML templates--> <dependency> <groupId>org.springframework.b ...
- pwa 概念
- 通过python构建集中式的病毒扫描机制
Clam AntiVirus(Clam AV)是一个免费而且开放源码的防毒软件,软件与病毒库的更新由开源社区免费发布,目前ClamdAV主要为Linux.Uinux系统提供病毒扫描查杀pyClamad ...
- log4j显示hibernate sql参数的配置
#下面的两条配置非常重要,设置为trace后,将可以看到打印出sql中 ? 占位符的实际内容 #this is the most important config for showing parame ...
- eclipse 使用tomcat7.0建立Dynamic Web Project 时 web.xml的问题
最近使用Eclipse helios版本结合tomcat7.0建立动态的web项目时,发现在WEB-INF下的web.xml没有了. 解决方案: 建立web项目时,建到第三个下一步时,将 Genera ...
- C++17尝鲜:类模板中的模板参数自动推导
模板参数自动推导 在C++17之前,类模板构造器的模板参数是不能像函数模板的模板参数那样被自动推导的,比如我们无法写 std::pair a{1, "a"s}; // C++17 ...
- [转]Gulp思维 —— Gulp高级技巧
感受过gulp.js带来的兴奋过后,你需要的不仅仅是它的光鲜,而是切切实实的实例.这篇文章讨论了一些使用gulp.js时常踩的坑,以及一些更加高级和定制化的插件和流的使用技巧. 基本任务 gulp的基 ...
- delphi修改QQ快捷方式的目标地址达到在启动QQ的同时也能运行自己想要启动的EXE可执行文件
delphi修改QQ快捷方式的目标地址达到在启动QQ的同时也能运行自己想要启动的EXE可执行文件. 直接上代码,自已体会 !! Unit1.pas代码如下: unit Unit1; interface ...
- C#实现支持单点登录的一个存储用户信息的类
网上有很多介绍单点登录的文章,但多为架构设计以及概念性文章,而本文将介绍单点登录的具体具体实现 利用哈希表,作为保存登录用户的队列 private static Hashtable m_ ...