新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythonkeys()
python 中的keys()函數(shù)返回一個(gè)視圖對(duì)象,該對(duì)象以列表形式顯示字典中的所有鍵。

**dict.keys()**
鍵()參數(shù):
keys()不接受任何參數(shù)。當(dāng)字典更新時(shí),它將反映出進(jìn)行這些更改的鍵。
鍵()返回值
如果我們對(duì)字典進(jìn)行任何更改,它也會(huì)反映視圖對(duì)象。如果字典是空的,它會(huì)返回一個(gè)空列表。
| 投入 | 返回值 | | 字典 | 查看對(duì)象 |
Python 中key()方法的示例
示例keys()在 Python 中是如何工作的?
persondet = {'name': 'Albert', 'age': 30, 'salary': 5000.0}
print(persondet.keys())
empty_dict
print(empty_dict.keys())
輸出:
dict_keys(['name', 'salary', 'age'])
dict_keys([])
示例 2:字典更新時(shí)鍵()的工作?
persondet = {'name': 'Albert', 'age': 30, }
print('Before dictionary is updated')
keys = persondet.keys()
print(keys)
# adding an element to the dictionary
persondet.update({'salary': 5000.0})
print('\nAfter dictionary is updated')
print(keys)
輸出:
Before dictionary is updated
dict_keys(['name', 'age'])
After dictionary is updated
dict_keys(['name', 'age', 'salary']) 本文題目:創(chuàng)新互聯(lián)Python教程:Pythonkeys()
文章轉(zhuǎn)載:http://www.dlmjj.cn/article/dhihjdi.html


咨詢
建站咨詢
