亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

Python 基礎(chǔ)教程

Python 流程控制

Python 函數(shù)

Python 數(shù)據(jù)類型

Python 文件操作

Python 對象和類

Python 日期和時間

Python 高級知識

Python 參考手冊

Python 字典 items() 使用方法及示例

Python 字典方法

items()方法返回一個視圖對象,該對象顯示字典的(鍵,值)元組對的列表。

items()方法的語法為:

dictionary.items()

items()方法類似于Python 2.7中的dictionary  viewitems()方法

items()參數(shù)

items()方法不帶任何參數(shù)。

從items()返回值

items()方法函數(shù)以列表返回可遍歷的(鍵, 值) 元組數(shù)組。

示例1:使用items()獲取字典中的所有項目

# 隨機銷售字典
sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }

print(sales.items())

運行該程序時,輸出為:

dict_items([('apple', 2), ('orange', 3), ('grapes', 4)])

示例2:修改字典后items()如何工作?

# 隨機銷售字典
sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }

items = sales.items()
print('原始的items:', items)

# 從字典中刪除一個項目
del[sales['apple']]
print('更新后的items:', items)

運行該程序時,輸出為:

原始的items: dict_items([('apple', 2), ('orange', 3), ('grapes', 4)])
更新后的items: dict_items([('orange', 3), ('grapes', 4)])

該視圖對象items本身并不返回銷售項目列表,而是返回一個sales的(鍵,值)對的視圖。

如果列表隨時更新,則更改將反映到視圖對象本身,如上面的程序所示。

Python 字典方法