如何將 Python 字典和列表壓縮到一起?


在本文中,我們將向您展示如何將 Python 字典和列表壓縮到一起。以下是完成此任務的各種方法 -

  • 使用 zip() 函式

  • 使用 append() 函式和 items() 函式

  • 使用 append() 函式和 in 運算子。

使用 zip() 將字典和列表組合在一起

演算法(步驟)

以下是執行所需任務應遵循的演算法/步驟 -

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

  • 建立另一個變數來儲存輸入列表。

  • 使用zip() 函式(zip() 函式可用於組合兩個列表/迭代器)透過使用items() 函式(返回字典的鍵值對)將輸入字典和列表的鍵值對以及輸入列表作為引數傳遞給它來將輸入字典和列表組合在一起。

  • 它將結果作為給定字典和輸入列表的 zip 物件返回。

  • 透過使用list() 函式(返回迭代物件的列表)將上述 zip 物件轉換為列表來列印結果 zip 結果。

示例

以下程式使用 zip() 函式將輸入字典和列表組合在一起 -

# input dictionary inputDict = {'Hello':1, 'tutorialspoint':2, 'python':3, 'codes':3} # input list inputList = [10, 20, 30, 40] # combining input dictionary and list together zippedResult = zip(inputDict.items(), inputList) print("Combining input dictionary and list together:") # printing the resultant zip result by converting the # above zip object into a list print(list(zippedResult))

輸出

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

Combining input dictionary and list together:
[(('Hello', 1), 10), (('tutorialspoint', 2), 20), (('python', 3), 30), (('codes', 3), 40)]

建立 Zip 物件並訪問它

演算法(步驟)

以下是執行所需任務應遵循的演算法/步驟 -

  • 使用for 迴圈遍歷上述 zip 物件中輸入字典的鍵值對和列表項。

  • 列印字典的對應鍵。

  • 列印字典的對應值。

  • 列印輸入列表的對應列表項。

示例

以下程式使用zip() 函式將輸入字典和列表組合在一起,並列印字典的鍵、值和壓縮物件的列表項 -

# input dictionary inputDict = {'Hello':1, 'tutorialspoint':2, 'python':3, 'codes':3} # input list inputList = [10, 20, 30, 40] # combining input dictionary and list together zippedObject = zip(inputDict.items(), inputList) print("Zipping input dictionary and list:") # traversing through key, value pair of dictionary and list items in # the above zip object for (k, v), listItem in zippedObject: # printing the corresponding key of a dictionary print(k) # printing the corresponding value of a dictionary print(v) # printing corresponding list item of the input list print(listItem)

輸出

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

Zipping input dictionary and list:
Hello
1
10
tutorialspoint
2
20
python
3
30
codes
3
40

使用 append() 和 items() 函式

演算法(步驟)

以下是執行所需任務應遵循的演算法/步驟 -

  • 獲取一個空列表以儲存給定字典和列表的 zip 物件。

  • 獲取列表的索引值並將其值初始化為 0。

  • 使用items() 函式遍歷字典的鍵值對。

  • 建立字典的鍵和值的元組,並將其儲存在一個變數中。

  • 使用append() 函式將上述元組和當前索引處的列表元素作為元組追加到列表中。

  • 將列表索引的值加 1。

  • 列印字典和列表的 Zip 物件。

示例

以下程式使用append()items() 函式將輸入字典和列表組合在一起 -

# input dictionary inputDict = {'Hello':1, 'tutorialspoint':2, 'python':3, 'codes':3} # input list inputList = [10, 20, 30, 40] # empty list for storing result result = [] # initializing a variable of list index with 0 i=0 # traversing through key, value pair of input dictionary for key,value in inputDict.items(): # creating a tuple of key-value pairs of a dictionary dict_key_value_tuple = (key,value) # appending corresponding key, value pair of input dictionary, and input list element to the above result list result.append(((dict_key_value_tuple),inputList[i])) # incrementing the value of the list index by 1 i+=1 # printing the resultant zip of input dictionary and list print("Zipping input dictionary and list:\n", result)

輸出

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

Zipping input dictionary and list:
[(('Hello', 1), 10), (('tutorialspoint', 2), 20), (('python', 3), 30), (('codes', 3), 40)]

使用 append() 函式和 in 運算子

示例

以下程式使用append() 函式和 in 運算子將輸入字典和列表組合在一起 -

# input dictionary inputDict = {'Hello':1, 'tutorialspoint':2, 'python':3, 'codes':3} # input list inputList = [10, 20, 30, 40] # empty list for storing result result = [] # initializing a variable of list index with 0 i=0 # traversing through key of input dictionary for key in inputDict: # creating a tuple of key-value pair of a dictionary # here inputDict[key] represents value of a dictionary dict_key_value_tuple = (key,inputDict[key]) # appending corresponding key, value pair of input dictionary, # input list element as tuple to the above result list result.append(((dict_key_value_tuple),inputList[i])) # incrementing the value of the list index by 1 i+=1 # printing the resultant zip of input dictionary and list print("Zipping input dictionary and list:\n", result)

輸出

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

Zipping input dictionary and list:
[(('Hello', 1), 10), (('tutorialspoint', 2), 20), (('python', 3), 30), (('codes', 3), 40)]

結論

在本文中,我們研究了三種在 Python 中壓縮給定字典或列表的技術。我們學習瞭如何訪問 zip 物件並顯示其值。我們還學習瞭如何使用 items() 函式和 in 運算子遍歷字典。

更新於: 2022 年 11 月 9 日

12K+ 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告