items()方法

items()方法

描述

字典 items() 方法以列表形式(并非直接的列表,若要返回列表值还需调用list函数)返回可遍历的(键, 值) 元组数组。

语法

 items() 方法语法:

D.items()

参数

  • 无。

返回值

以列表形式返回可遍历的(键, 值) 元组数组。

实例

D = {'Google': 'www.google.com', 'Runoob': 'www.runoob.com', 'taobao': 'www.taobao.com'}
 
print("字典值 : %s" % D.items())
print("转换为列表 : %s" % list(D.items()))
 
# 遍历字典列表
for key, value in D.items():
    print(key, value)

以上实例输出结果为:

字典值 : D_items([('Google', 'www.google.com'), ('taobao', 'www.taobao.com'), ('Runoob', 'www.runoob.com')])
转换为列表 : [('Google', 'www.google.com'), ('taobao', 'www.taobao.com'), ('Runoob', 'www.runoob.com')]
Google www.google.com
taobao www.taobao.com
Runoob www.runoob.com

猜你喜欢

转载自www.cnblogs.com/xiaohei001/p/10193548.html