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


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

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

  • 使用 [] 和 *

  • 使用列表推導式

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

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

方法 1:使用 dict.values() & list() 函式

為了獲得列表,我們可以結合使用dictionary.values()list() 函式values() 方法用於訪問鍵值對中的值,然後使用list() 函式將這些值轉換為列表。

演算法(步驟)

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

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

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

示例

以下程式使用 len() 函式返回列表的大小:

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

輸出

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

['TutorialsPoint', 'Python', 'Codes']

方法 2:使用 [] 和 *

我們可以透過使用 [] 和 * 來獲取字典值的完整列表。在這裡,values() 是一個字典方法,用於從字典中的鍵值對中檢索值,* 用於僅獲取值而不是字典值,然後我們使用list() 函式將其轉換為列表。

演算法(步驟)

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

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

  • 使用values() 函式(values() 方法返回一個檢視物件。字典值作為列表儲存在檢視物件中)、[]、* 運算子列印字典所有值的列表。

示例

以下程式使用 [] 和 * 運算子返回字典所有值的列表

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Printing the list of values of a dictionary with values() function # and * operator print([*demoDictionary.values()])

輸出

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

['TutorialsPoint', 'Python', 'Codes']

方法 3:使用列表推導式

為了查詢值的列表,此方法使用列表推導式 技術。它接受一個鍵作為輸入,並使用for 迴圈返回一個列表,其中包含列表中每個字典中每個鍵出現的對應值。與其他方法相比,它更優雅,更符合Python風格。

演算法(步驟)

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

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

  • 透過遍歷字典的每個值,使用列表推導式獲取所有值的列表。

demoDictionary[dict_key] represents dictionary value
  • 列印輸入字典所有值的列表。

示例

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

# input dictionary demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'} # Getting the list of values of the dictionary with the list comprehension # demoDictionary[dict_key] represents dictionary value dict_valuesList = [demoDictionary[dict_key] for dict_key in demoDictionary] # Printing the list of all the values of a dictionary print(dict_valuesList)

輸出

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

['TutorialsPoint', 'Python', 'Codes']

方法 4:使用 append() 函式 & for 迴圈

演算法(步驟)

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

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

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

  • 使用 for 迴圈,使用values() 函式遍歷字典的所有值(values() 方法返回一個檢視物件。字典值作為列表儲存在檢視物件中)。

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

  • 列印字典中所有值的列表。

示例

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

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

輸出

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

['TutorialsPoint', 'Python', 'Codes']

我們使用一個空列表來儲存字典的值,然後迭代這些值,使用append() 函式將它們新增到列表中,並顯示它們。

結論

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

更新於:2023年11月2日

21K+ 瀏覽量

啟動您的職業生涯

完成課程獲得認證

開始
廣告