Python程式:為具有最大元素索引的鍵賦值


在Python中,元素索引指的是元素在一個序列(例如列表或字串)中的位置。它表示元素的位置,從第一個元素的0開始,每個後續元素遞增1。為具有最大元素索引的鍵賦值意味著將唯一的識別符號或標籤與序列中可能的最大索引值相關聯。這種方法允許使用分配的鍵輕鬆訪問和檢索基於其位置的元素。

示例

假設我們已經得到了一個輸入字典。我們現在將找到每個鍵的值中的最大元素,並將它的索引分配給鍵,然後使用上述方法列印結果字典。

輸入

inputDict = {'hello': [4, 2, 8],
			'tutorialspoint': [5, 12, 10],
			'python': [9, 3, 7],
			'users': [3, 6, 1]}

輸出

Resultant dictionary after assigning keys with maximum element index:
 {'hello': 2, 'tutorialspoint': 1, 'python': 0, 'users': 1}

在上面的輸入字典中,鍵hello的最大元素是8,其索引為2。因此,最大元素的索引被分配給鍵hello,值為2,其他元素也是如此,然後列印結果字典。

hello: 最大元素是 8 -> 索引是 2

'tutorialspoint': 最大元素是 12 -> 索引是 1

'python': 最大元素是 9 -> 索引是 0

'users': 最大元素是 6 -> 索引是 1

index() 函式

index() 函式返回提供的值在第一次出現時的位置。

語法

list.index(element)

max() 函式

max() 函式返回可迭代物件中最高值項/最大數。

演算法(步驟)

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

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

  • 列印輸入字典。

  • 使用dict()函式建立一個空字典,用於儲存結果字典。

  • 使用for迴圈遍歷輸入字典的鍵。

  • 獲取字典鍵中最大元素的索引,並將相同的鍵與最大元素索引一起儲存。

  • 為每個鍵分配最大元素索引後,列印結果字典。

示例 1:使用 for 迴圈、index() 和 max() 函式

以下程式使用 for 迴圈、index() 和 max() 函式,在為輸入字典的每個鍵分配最大元素索引後返回一個字典

示例

# input dictionary
inputDict = {'hello': [4, 2, 8],
             'tutorialspoint': [5, 12, 10],
             'python': [9, 3, 7],
             'users': [3, 6, 1]}
# printing input dictionary
print("Input dictionary:\n", inputDict)
# empty dictionary for storing a resultant dictionary
resultantDict = dict()
# traversing through the keys of the input dictionary
for k in inputDict:
	# Getting the maximum element index 
    # Storing it as a value for the same key
    resultantDict[k] = inputDict[k].index(max(inputDict[k]))
# printing resultant dictionary
print("Resultant dictionary after assigning keys with maximum element index:\n", resultantDict)

輸出

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

Input dictionary:
 {'hello': [4, 2, 8], 'tutorialspoint': [5, 12, 10], 'python': [9, 3, 7], 'users': [3, 6, 1]}
Resultant dictionary after assigning keys with maximum element index:
 {'hello': 2, 'tutorialspoint': 1, 'python': 0, 'users': 1}

示例 2:使用字典推導式、index() 和 max() 函式

在這個例子中,我們使用字典推導式,它是上面 for 迴圈的簡寫版本。

以下程式使用字典推導式、index() 和 max() 函式,在為輸入字典的每個鍵分配最大元素索引後返回一個字典

示例

# input dictionary
inputDict = {'hello': [4, 2, 8],
             'tutorialspoint': [5, 12, 10],
             'python': [9, 3, 7],
             'users': [3, 6, 1]}
# printing input dictionary
print("Input dictionary:\n", inputDict)
# Performing same using dictionary comprehension for concise syntax
resultantDict = {k: inputDict[k].index(max(inputDict[k])) for k in inputDict}
# printing resultant dictionary
print("Resultant dictionary after assigning ks with maximum element index:\n",
      resultantDict)

輸出

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

Input dictionary:
 {'hello': [4, 2, 8], 'tutorialspoint': [5, 12, 10], 'python': [9, 3, 7], 'users': [3, 6, 1]}
Resultant dictionary after assigning ks with maximum element index:
 {'hello': 2, 'tutorialspoint': 1, 'python': 0, 'users': 1}

結論

在本文中,我們學習了兩種不同的方法來為具有最大元素索引的鍵賦值。我們學習瞭如何找到字典中最高值的元素及其索引。最後,我們學習瞭如何使用字典推導式來編寫簡潔的語法。

更新於:2023年8月17日

79 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.