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

**dict.keys()**
鍵()參數(shù):
keys()不接受任何參數(shù)。當(dāng)字典更新時,它將反映出進(jìn)行這些更改的鍵。
鍵()返回值
如果我們對字典進(jìn)行任何更改,它也會反映視圖對象。如果字典是空的,它會返回一個空列表。
| 投入 | 返回值 | | 字典 | 查看對象 |
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:字典更新時鍵()的工作?
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']) 新聞標(biāo)題:創(chuàng)新互聯(lián)Python教程:Pythonkeys()
網(wǎng)站鏈接:http://m.fisionsoft.com.cn/article/dhihjdi.html


咨詢
建站咨詢
