Python程式:將列表元素交替作為鍵值對


在本文中,我們將學習如何在Python中將列表元素交替作為鍵值對。

假設我們已經得到了一個包含整數的輸入列表。我們現在將

使用的方法

以下是完成此任務所使用的各種方法:

  • 使用for迴圈

  • 使用字典推導和列表切片

示例

假設我們已經得到了一個輸入列表。我們現在將列印交替的列表元素作為鍵值對。

輸入

inputList = [20, 13, 5, 2, 6, 8, 18, 3]

輸出

Dictionary with alternate list elements as key-value pairs:
{13: 2, 2: 8, 8: 3, 20: 5, 5: 6, 6: 18}

交替元素被對映以獲得鍵值對。13 -> 2 [交替]

方法1:使用For迴圈

演算法(步驟)

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

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

  • 列印輸入列表。

  • 建立一個空字典來儲存結果字典。

  • 使用for迴圈遍歷輸入列表的長度,但不包括最後一個索引,使用range()和len()函式(返回物件中的專案數)。

  • range()函式返回一個數字序列,該序列從0開始,以1(預設)遞增,並在到達給定數字之前停止。

  • 使用if條件語句結合取模運算子%(返回餘數)來檢查當前索引是否為偶數。

  • 將偶數索引元素作為鍵值對賦值給輸出字典。

  • 同樣,使用非邏輯運算子檢查奇數索引,獲取另一組奇數索引元素。

  • 列印結果字典,其中包含作為鍵值對的交替列表元素。

示例

以下程式使用for迴圈返回一個包含交替列表元素作為鍵值對的字典:

# input list
inputList = [20, 13, 5, 2, 6, 8, 18, 3]

# printing the given input list
print("Input List: ", inputList)

# creating an empty dictionary for storing resultant dict
outputDict = dict()

# =traversing till the length of the input list except for the last index
for i in range(len(inputList) - 2):

   # checking whether the current index is even or not
   if i % 2:
         
      # assigning even index elements to output dict as a key-value pairs
      outputDict[inputList[i]] = inputList[i + 2]

# getting the other set with odd index elements
for i in range(len(inputList) - 2):
   if not i % 2:
      outputDict[inputList[i]] = inputList[i + 2]

# printing the resultant dictionary with alternate list elements as key-value pairs
print("Dictionary with alternate list elements as key-value pairs:\n", outputDict)

輸出

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

Input List: [20, 13, 5, 2, 6, 8, 18, 3]
Dictionary with alternate list elements as key-value pairs:
{13: 2, 2: 8, 8: 3, 20: 5, 5: 6, 6: 18}

方法2:使用字典推導和列表切片

Python支援字典推導,就像列表推導一樣。可以使用簡單的表示式建立字典。字典推導的公式為:

{key: value for (key, value) in iterable}

列表切片是一種常見的做法,程式設計師最常使用它來有效地解決問題。考慮一個Python列表。您必須切片列表才能訪問一系列列表元素。使用冒號(:),一個簡單的切片運算子,是一種實現此目的的方法。

語法

[start:stop:step]

引數

  • start - 開始索引

  • end - 結束索引

  • step - 要跳過的數字,即步長

演算法(步驟)

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

  • 建立一個變數來儲存輸入列表,並列印給定的輸入列表。

  • 使用切片從輸入列表中獲取所有奇數索引元素,起始值為1,步長值為2。

  • 使用切片從輸入列表中獲取所有偶數索引元素,起始值為0,步長值為2。

  • 使用字典推導透過遍歷奇數列表來獲取奇數索引列表元素作為鍵值對

  • 將偶數索引列表元素作為鍵值對新增到上述輸出字典中。

  • update()函式(將給定的項,例如字典或具有鍵值對的可迭代物件插入到字典中)。

  • 列印結果字典,其中包含作為鍵值對的交替列表元素。

示例

以下程式使用字典推導和列表切片返回一個包含交替列表元素作為鍵值對的字典:

# input list
inputList = [20, 13, 5, 2, 6, 8, 18, 3]

# printing the given input list
print("Input List: ", inputList)

# getting all odd index elements from input list using slicing
oddList = inputList[1::2]

# getting all odd index elements from input list using slicing
evenList = inputList[::2]

# getting odd index list elements as key-value pairs
outputDict = {oddList[i]: oddList[i + 1] for i in range(len(oddList) - 1)}

#updating(adding) even index list elements as key-value pairs to the outputDict
outputDict.update({evenList[i]: evenList[i + 1]
for i in range(len(evenList) - 1)})
   
   # printing the resultant dictionary with alternate list elements as key-value pairs
print("Dictionary with alternate list elements as key-value pairs:\n", outputDict)

輸出

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

Input List: [20, 13, 5, 2, 6, 8, 18, 3]
Dictionary with alternate list elements as key-value pairs:
{13: 2, 2: 8, 8: 3, 20: 5, 5: 6, 6: 18}

結論

本文介紹了兩種不同的方法來獲取作為鍵值對的交替列表元素。我們學習瞭如何使用步長值來切片像列表、元組等可迭代物件。此外,我們學習了字典推導。最後,我們學習瞭如何使用update()函式新增或更新字典。

更新於:2023年1月27日

487 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告