#!/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. SQL SERVER 2012/ 2014 分页,用 OFFSET,FETCH NEXT改写ROW_NUMBER的用法

    写法: 假装有个表Shop,其中有一列ShopName,取100000到100050条数据. ROW_NUMBER 的写法 SELECT * FROM ( SELECT ShopName , ROW_ ...

  2. MySQL数据库索引(中)

    上一篇回顾: 1.一个索引对应一颗B+树,所有的真实记录都是存在叶子节点里面的,所有的项目录都存在内节点或者说根节点上. 2.innodb会为我们的表格主键添加一个聚簇索引,如果没有主键的话数据库是会 ...

  3. CYQ.Data 数据框架 使用篇一 入门指南---001

    原文链接:http://www.cyqdata.com/cyqdata/article-detail-411 本文针对V5版本进行修改于(2016-07-04) 下面是使用步骤: 一:下载框架DLL[ ...

  4. python编程之禅

    在python界面输入 import this >>> import this The Zen of Python, by Tim Peters Beautiful is bette ...

  5. Java并发知识(1)

    1. 进程和线程之间有什么不同? 一个进程是一个独立(self contained)的运行环境,它可以被看作一个程序或者一个应用.而线程是在进程中执行的一个任务.Java运行环境是一个包含了不同的类和 ...

  6. The requested resource (/) is not available解决办法

    The requested resource (/) is not available HTTP Status 404(The requested resource is not available) ...

  7. php 利用迭代器遍历文件夹

    1.遍历文件夹 scandir 2.原生的迭代器Iterrate $scan_dir = "txtDir"; //下面会遍历txtDir 下面所有字文件夹中的文件哦 $dir_it ...

  8. github page更新后不生效

    昨晚在本地git仓库修改了页面内容后,git push上去,到页面去刷新发现,并没有改变.本来还想着是需要点时间来更新,就再等等. 没想到过了十几分钟后,还是没有更新. 然后同时习惯性地打开了邮箱,发 ...

  9. js join 与 split

    var a = [] var b = [1,2,3] b.push('4')   // b = [1,2,3,4] a = b.join('-')  // a = '1-2-3-4' b = a.sp ...

  10. Top K算法

    应用场景: 搜索引擎会通过日志文件把用户每次检索使用的所有检索串都记录下来,每个查询串的长度为1-255字节.        假设目前有一千万个记录(这些查询串的重复度比较高,虽然总数是1千万,但如果 ...