#!/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字典练习的更多相关文章

  1. Python字典和集合

    Python字典操作与遍历: 1.http://www.cnblogs.com/rubylouvre/archive/2011/06/19/2084739.html 2.http://5iqiong. ...

  2. python 字典排序 关于sort()、reversed()、sorted()

    一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...

  3. python字典中的元素类型

    python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...

  4. python字典copy()方法

    python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...

  5. python 字典实现类似c的switch case

    #python 字典实现类似c的switch def print_hi(): print('hi') def print_hello(): print('hello') def print_goodb ...

  6. python字典的常用操作方法

    Python字典是另一种可变容器模型(无序),且可存储任意类型对象,如字符串.数字.元组等其他容器模型.本文章主要介绍Python中字典(Dict)的详解操作方法,包含创建.访问.删除.其它操作等,需 ...

  7. Python 字典(Dictionary)操作详解

    Python 字典(Dictionary)的详细操作方法. Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字 ...

  8. Python 字典(Dictionary) get()方法

    描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...

  9. Python 字典(Dictionary) setdefault()方法

    描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...

  10. python 字典内置方法get应用

    python字典内置方法get应用,如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这 ...

随机推荐

  1. JPA注解@SecondaryTables 实现一个实体映射多张数据库表

    参考:http://jingpin.jikexueyuan.com/article/46978.html Annotation Type SecondaryTables(参考:https://docs ...

  2. Eclipse安装STS(Spring Tool Suite (STS) for Eclipse)插件

    转自:https://blog.csdn.net/zhen_6137/article/details/79383941

  3. SQL 2012 分页取数据

    ,), data int ) select * from t1 row rows only create clustered index t1c on t1(id) declare @i int ) ...

  4. js倒计时发送验证码按钮

    var wait=60; function time(o) { if (wait == 0) { o.removeAttribute("disabled"); o.value=&q ...

  5. 五种方法实现Java的Singleton单例模式

    面试的时候经常会问到Java的单例模式,这道题能很好的考察候选人对知识点的理解程度.单例模式要求在系统运行时,只存在唯一的一个实例对象. 下面我们来详细剖析一下其中的关键知识点,并介绍五种实现方法,以 ...

  6. iOS 真机调试 Xcode 显示 device Error: device unavailable

    一般来说有两个原因: 1. iphone没有加到test device里,将iphone的设备id加到test device列表里 2. Xcode不支持当前的iOS版本,将Xcode升级到最新版

  7. springboot测试时 SpringApplicationConfiguration注解不能用

    测试时,@SpringApplicationConfiguration(classes = Application.class) 报错,注解不能导入. 在学习spring boot时,按照文档学习时测 ...

  8. box2d 易错

    1.动态刚体与一个与静态刚体重叠的小的感应刚体在contactBegin时,有些时候无法侦测到 2.bodyA.GetContactList()应用非动态刚体,会找不到另一非动态刚体的接触,非动态刚体 ...

  9. NISP视频知识点总结

    身份认证访问控制安全审计本章实验 ===密码学=====古典密码 算法本身的保密性近代密码 机械密码\机电 密码打字密码机轮转机现代密码 基于密钥公钥密码 公钥==================对称 ...

  10. hadoop+zookeeper(ha架构搭建)

    http://blog.csdn.net/baidu_25820069/article/details/52225293 [条件所限,待验证]