您的位置:首页 > 编程语言 > Python开发

笨办法学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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: