笨办法学python(习题39)——items()、get()、
2018-01-01 10:21
399 查看
Python 字典(Dictionary) items()方法
描述
Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组。语法
items()方法语法:dict.items()
参数
NA。返回值
返回可遍历的(键, 值) 元组数组。实例
以下实例展示了 items()函数的使用方法:实例(Python 2.0+)
#!/usr/bin/python#coding=utf-8dict = {'Google':'www.google.com','Runoob':'www.runoob.com','taobao':'www.taobao.com'}print"字典值 : %s"% dict.items()# 遍历字典列表forkey,valuesindict.items():printkey,values以上实例输出结果为:字典值 : [('Google', 'www.google.com'), ('taobao', 'www.taobao.com'), ('Runoob', 'www.runoob.com')] Google www.google.com taobao www.taobao.com Runoob www.runoob.com
描述
Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值。语法
get()方法语法:dict.get(key, default=None)参数
key -- 字典中要查找的键。default -- 如果指定键的值不存在时,返回该默认值值。返回值
返回指定键的值,如果值不在字典中返回默认值None。实例
以下实例展示了 get()函数的使用方法:#!/usr/bin/pythondict = {'Name': 'Zara', 'Age': 27}print "Value : %s" % dict.get('Age')print "Value : %s" % dict.get('Sex', "Never")以上实例输出结果为:Value : 27Value : Never相关文章推荐
- 笨办法学python加分习题39
- 笨办法学Python-习题37-39
- 笨办法学python加分习题28
- 笨办法学Python(习题38)——len()、join()、x.[x,x]
- 笨办法学Python-习题1
- 笨办法学python 习题0中 shell无法调用python
- 笨办法学python习题1第一个程序
- learn python the hard way(笨办法学python) 练习39
- JS中“null'document.getElementById为空或不是对象” 的解决办法
- 笨办法学python加分习题29
- 【笨办法学Python】习题11:打印出改变了的输入
- 笨办法学python加分习题35
- python UnicodeEncodeError: 'ascii' codec can't encode characters 解决办法
- 笨办法学python习题18 命名、变量、代码和函数
- python name 'file' is not defined的解决办法
- 笨办法学python加分习题16
- 笨办法学Python习题11 提问
- 笨办法学python笔记(习题35)——exit()用法
- 笨办法学python 习题46
- 笨办法学python习题16 读写文件