日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python字典中items()函數用法實例_python

作者:工具人01 ? 更新時間: 2022-12-25 編程語言

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

將字典中的鍵值對以元組存儲,并將眾多元組存在列表中。

如:

favorite_places = {'XiaoMing': 'TJL','XiaoQiang':'Amercia','Dongsheng':'Japan'}
print("數值:%s" % favorite_places.items())
for key,value in favorite_places.items():
    print(key,value)

數值:dict_items([('XiaoQiang', 'Amercia'), ('Dongsheng', 'Japan'), ('XiaoMing', 'TJL')])
XiaoQiang Amercia
Dongsheng Japan
XiaoMing TJL

具體可以參考以下的例子,十分明顯

python3.5中的解釋:
def items(self): # real signature unknown; restored from __doc__
    """ D.items() -> a set-like object providing a view on D's items """
    pass

附:實例

dict = {'老大':'15歲',
        '老二':'14歲',
        '老三':'2歲',
        '老四':'在墻上'
        }
print(dict.items())
for key,values in dict.items():
    print(key + '已經' + values + '了')

以上實例輸出結果為:

dict_items([('老大', '15歲'), ('老二', '14歲'), ('老三', '2歲'), ('老四', '在墻上')])
老大已經15歲了
老二已經14歲了
老三已經2歲了
老四已經在墻上了了

總結

原文鏈接:https://blog.csdn.net/hellfu/article/details/110875605

欄目分類
最近更新