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的Samba开源共享解决方案测试(一)
转自http://blog.csdn.net/u013394982/article/details/17914429 Linux操作系统 Linux是一类Unix计算机操作系统的统称.Linux操作系 ...
- HTML5 新特性(一)
1. 新的Doctype 尽管使用<!DOCTYPE html>,即使浏览器不懂这句话也会按照标准模式去渲染 2. Figure元素 用<figure>和<figcapt ...
- 内置锁(一)synchronized 介绍与用法
一.synchronized 的介绍 synchronized 是 Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码,而这段代码也被称 ...
- List集合的clear方法
一 . list.clear()底层源码实现 在使用list 结合的时候习惯了 list=null :在创建这样的方式,但是发现使用list的clear 方法很不错,尤其是有大量循环的时候 1.lis ...
- leetcode476
public class Solution { public int FindComplement(int num) { //计算数字二进制的反码 var list = new List<int ...
- leader 选举机制
from: http://www.jasongj.com/2015/01/02/Kafka%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90/ 一种非常常用的选举leader的方 ...
- Haskell语言学习笔记(33)Exception, Except, ExceptT
Exception class (Typeable e, Show e) => Exception e where toException :: e -> SomeException fr ...
- asp.net cors solution
I have a simple actionmethod, that returns some json. It runs on ajax.example.com. I need to access ...
- ASP.Net 下载大文件的实现
当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃.可以参考如下代码来避免这个问题. 关于此代码的几点说明: 1. 将数据分成较小的部分,然后将其移 ...
- hibernate中.常见的hql查询语句
hql是非常有意识的被设计为完全面向对象的查询 基本规则: 1.hql语法类似于sql,但它后面跟的不是表名和字段名,而是类名和属性名 2.hql大小写不敏感.但是设计java类名,包名,属性名时大小 ...