1、get()函数
用法:dict.get(key, default=None) key -- 字典中要查找的键。default -- 如果指定键的值不存在时,返回该默认值
例如:
1 import numpy as np
2 dic1 = {"国家":'中国',"民族":'汉族',"GDP排名":2}
3 nation=dic1.get("国家",0)
4 >>>print(nation)
5 >>>中国
6 default = dic1.get("城市","没有对应的值")
7 >>>print(default)
8 >>>没有对应的值
2、items()函数
Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组。
用法:dict.items()
示例:
1 dict = {'Google': 'www.google.com', 'Runoob': 'www.runoob.com', 'taobao': 'www.taobao.com'}
2
3 print "字典值 : %s" % dict.items()
4
5 # 遍历字典列表
6 for key,values in dict.items():
7 print key,values
输出结果为:
1 字典值 : [('Google', 'www.google.com'), ('taobao', 'www.taobao.com'), ('Runoob', 'www.runoob.com')]
2 Google www.google.com
3 taobao www.taobao.com
4 Runoob www.runoob.com
以后慢慢补充。。。。