如何從Python字典中獲取所有鍵的列表?


在本文中,我們將向您展示如何使用各種方法從Python字典中獲取所有鍵的列表。我們可以使用以下方法獲取Python字典中所有鍵的列表:

  • 使用dict.keys()方法

  • 使用list() & dict.keys()函式

  • 使用列表推導式

  • 使用解包運算子(*)

  • 使用append()函式 & For迴圈

假設我們已經得到一個示例字典。我們將使用上面指定的不同方法返回Python字典中所有鍵的列表。

方法1:使用dict.keys()方法

在Python字典中,dict.keys()方法提供一個檢視物件,該物件按插入順序顯示字典中所有鍵的列表

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入字典,並向其中新增一些隨機的鍵值對。

  • 使用keys()函式並將其應用於輸入字典以獲取字典所有鍵的列表並列印它。

示例

以下程式使用keys()函式返回字典所有鍵的列表:

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Printing the list of keys of a dictionary using keys() function print(demoDictionary.keys())

輸出

執行上述程式後,將生成以下輸出:

dict_keys([10, 12, 14])

方法2:使用list() & dict.keys()函式

Python中的list()方法接受任何可迭代物件作為引數並返回一個列表。可迭代物件是Python中可以迭代的物件。

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入字典

  • 使用keys()函式(dict.keys()方法提供一個檢視物件,按插入順序顯示字典中所有鍵的列表)列印字典所有鍵的列表,將其應用於輸入字典,並使用list()函式(將序列/可迭代物件轉換為列表)將其結果轉換為列表。

示例

以下程式使用list()keys()函式返回字典所有鍵的列表:

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Printing the list of keys of a dictionary using keys() function # list() methods convert an iterable into a list print(list(demoDictionary.keys()))

輸出

[10, 12, 14]

方法3:使用列表推導式

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入字典。

  • 使用keys()函式並將其應用於輸入字典以獲取字典所有鍵的列表。

  • 使用列表推導式和for迴圈遍歷上述鍵列表中的每個鍵,列印字典的鍵列表。

示例

以下程式使用列表推導式返回字典所有鍵的列表:

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # getting all the keys from a dictionary dictionaryKeys = demoDictionary.keys() # Printing the list of keys of a dictionary by traversing through each key # in the above keys list using the list comprehension print([key for key in dictionaryKeys])

輸出

[10, 12, 14]

方法4:使用解包運算子(*)

解包運算子*適用於任何可迭代物件,並且由於字典在迭代時會給出它們的鍵,因此您可以輕鬆地使用它在列表文字中生成列表。

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入字典

  • 使用解包運算子(*)和以下語法列印字典所有鍵的列表(這裡將解包字典的所有鍵,並使用*作為解包運算子將其作為列表返回)

print([*demoDictionary])

示例

以下程式使用解包運算子(*)返回字典所有鍵的列表:

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Printing the list of keys of a dictionary by using the unpacking operator(*) print([*demoDictionary])

輸出

[10, 12, 14]

方法5:使用append()函式 & For迴圈

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入字典

  • 建立一個空列表來儲存輸入字典的所有鍵

  • 使用for迴圈,使用keys()函式遍歷字典的所有鍵

  • 使用append()函式(在列表末尾新增元素)將字典的每個鍵新增到列表中,並將相應的鍵作為引數傳遞給它。

  • 列印字典所有鍵的列表。

示例

以下程式使用append()函式 & for迴圈返回字典所有鍵的列表:

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # an empty list for storing dictionary keys dictKeysList = [] # Traversing through all the keys of the dictionary for dictKey in demoDictionary.keys(): # appending each key to the list dictKeysList.append(dictKey) # Printing the list of keys of a dictionary print(dictKeysList)

輸出

執行上述程式後,將生成以下輸出:

[10, 12, 14]

結論

本文教我們如何使用keys()函式獲取整個字典的鍵,以及如何使用list()函式將鍵轉換為列表。此外,我們還學習瞭如何在同一程式碼中使用列表推導式和for迴圈將keys()方法返回的字典中的鍵轉換為列表。最後,我們學習瞭如何使用append()函式(這裡我們將鍵新增到列表中)向列表中新增元素。

更新於:2023年8月25日

47K+ 瀏覽量

啟動您的職業生涯

完成課程後獲得認證

開始學習
廣告