#coding=utf-8
'''
type():返回字典类型
str():返回字典的字符串表示形式
cmp():比较函数。
cmp用于字典比较顺序是:字典的大小-->字典键------->字典值。
cmp()做字典的比较一般不是很有用。

len(mapping):返回映射的长度(键-值对的个数)
hash(obj):返回obj的哈希值
dict.clear():删除字典中所有元素
dict.copy():返回字典(浅复制)的一个副本
dict.get(key,default=None):对字典dict中的键,返回它对应的值value,
如果字典不存在此键,则返回default的值。
参数default默认值为None。

dict.items():返回一个包含字典中键值对元组的列表
dict.keys():返回一个包含字典中键的列表
dict.iter():方法iteritems()、iterkeys()、itervalues()与它们对应的非迭代方法一样,
不同的是它们返回一个迭代子,而不是一个列表。

dict.pop(key,[,default]):如果字典中key键存在,删除并返回dict[key],
如果key键不存在,且没有给出default的值,引发KeyError异常。

dict.setdefault(key,default=None):如果字典中不存在key键,由dict[key]=default为它赋值

dict.update(dict2):键字典dict2的键值对添加到字典dict中
dict.values():返回一个包含字典中所有值的列表。

字典中的值没有任何限制。字典中的键是有类型限制的。
每个键只能对应一个项,一键对应多个值是不允许的。
当有键发生冲突(字典键重复赋值),取最后(最近)的赋值。

键必须是可哈希的,所有不可变的类型都是可哈希的。
数字:值相等的数字表示相同的键。整型数字1和浮点数1.0的哈希值是相同的。
'''
#创建两个字典变量
from twisted.conch.test.test_agent import agent
dict1={"hello":"world","deep":"shallow","age":28}
dict2={"hello":"world","deep":"shallow","age":30}

#调用type函数
print "The type fo dict1 is ",type(dict1)

#调用str函数
print "The string of dict2 is :",str(dict2)

#调用cmp比较两个两个字典
#如果dict1大于dict2则输出大于0的值,相等输出0,小于输出小于0的值
print cmp(dict1,dict2)

#调用len函数,返回字典的长度(键值对的个数)
print "The length of dict2 :",len(dict2)

#调用hash函数返回对象的哈希值
#由于字典的键是可hash的对象
print "The hash value of dict1 :",hash(dict1.keys()[1])

#调用copy函数,返回字典的一个副本
dict3=dict1.copy()
print dict3
#调用clear函数,删除字典中的所有元素
dict3.clear()
print dict3

#调用get函数,获取字典中键对应的值
print "The value of the key hello in dict2 is :",dict2.get("hello")
print "The value of the key age in dict2 is :",dict2.get("age")
#如果字典中不存在键,则返回default的值,default默认值为None
print "The value of the key name in dict2 is :",dict2.get("name")
#如果字典中不存在键,则返回default的值,指定default值
print "The value of the key name in dict2 is :",dict2.get("name","ewang")

#调用items含糊,返回一个字典中键值对元组列表
print "The items of dict1 :",dict1.items()

#调用keys函数,返回一个包含字典中键的列表
print "The keys of the dict2 are :",dict2.keys()

#调用iter函数,在版本2.7中没有这个函数
#print "The iter of dict1 :",dict1.iter()

#调用pop函数,删除并返回键值
age=dict2.pop("age")
print "The age is ",age
#如果键不存在,且没有给出default的值,引发KeyError异常
try:
print dict2.pop("name")
except KeyError,e:
print "KeyError:",e
#如果键不存在,则返回default的值,指定default值
print dict2.pop("name","ewang")

#调用setdefault函数,给字典添加键值对
#只用于赋值,不能做修改
#如果不给default指定值,则默认为None
print dict2.setdefault("name")

#如果给default指定值,则返回default的值,指定default值
print dict2.setdefault("Name","badboy")
print dict2

#调用update函数把字典dict2的键值对添加到字典dict1中
print "before calling update :",dict1
dict1.update(dict2)
print "after call update :" ,dict1

#调用values函数,返回一个包含字典中所有值的列表
print "The values of dict1 :",dict1.values()