文章轉載於:https://www.cnblogs.com/wushuaishuai/p/7738118.html(博主:IT技術隨筆)
#Python3中已取消iteritems()方法
描述
Python 字典 items() 方法以列表形式(並非直接的列表,若要返回列表值還需調用list函數)返回可遍歷的(鍵, 值) 元組數組。
語法
items() 方法語法:
1
|
D.items()
|
參數
- 無。
返回值
以列表形式返回可遍歷的(鍵, 值) 元組數組。
實例
以下實例展示了 items() 方法的使用方法:
1
2
3
4
5
6
7
8
9
10
|
# !/usr/bin/python3
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)
|
以上實例輸出結果為:
1
2
3
4
5
|
字典值 : 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
|