Python程式:最小值鍵賦值
在Python中,值鍵賦值的含義是指在字典中將一個值與特定的鍵關聯起來的過程,允許根據鍵值對有效地檢索和操作資料。它支援為各種目的建立和組織結構化資料結構。
Python中實現的一種資料結構,更常見地稱為關聯陣列,是**字典**。字典由一組鍵值對組成。每個鍵值組合對應一個鍵及其相應的值。
示例
假設我們有兩個具有相同鍵的**輸入字典**。我們現在將比較兩個字典中每個鍵的值,並從中分配最小值給該鍵,並使用上述方法列印結果字典。
輸入
inputDict_1 = {"hello": 2, "tutorialspoint": 10, "users": 3} inputDict_2 = {"hello": 6, "tutorialspoint": 5, "users": 7}
輸出
Resultant dictionary after assigning minimum value keys: {'hello': 2, 'tutorialspoint': 5, 'users': 3}
在上面兩個字典中,2小於6,因此我們將**hello**鍵分配了最小值2。
同樣,對於**tutorialspoint**鍵,10大於5,因此5是最小值。
對於**users鍵**,3小於7,因此3是最小值。
使用的方法
以下是完成此任務的各種方法
使用字典推導式、min()和items()函式
使用dict()、min()和zip()函式
方法1:使用字典推導式、min()和items()函式
**min()**函式(返回可迭代物件中最小的/最小值)
**items()**函式(返回一個檢視物件,即它包含字典的鍵值對,作為列表中的元組)。
minimum_value = min(iterable) items_list = dictionary.items()
演算法(步驟)
以下是執行所需任務應遵循的演算法/步驟
建立一個變數來儲存輸入字典1。
建立另一個變數來儲存輸入字典2。
列印輸入字典。
使用items()函式遍歷第一個字典。
檢查第一個字典鍵和第二個字典值的最小值(由於鍵是公共的,因此我們可以直接使用[]運算子獲取它)。
使用字典推導式儲存具有最小值的公共鍵。
分配最小值鍵後,列印結果字典。
示例
以下程式使用字典推導式、min()和items()函式返回分配最小值鍵後的字典
# input dictionary 1 inputDict_1 = {"hello": 2, "tutorialspoint": 10, "users": 3} # input dictionary 2 inputDict_2 = {"hello": 6, "tutorialspoint": 5, "users": 7} # printing input dictionaries print("Input dictionary 1: ", inputDict_1) print("Input dictionary 2: ", inputDict_2) # Getting the minimum value of the keys from the first and second dictionaries. # Assigning them with the common key with the dictionary comprehension resultantDict = {k: min(v, inputDict_2[k]) for k, v in inputDict_1.items()} # printing resultant dictionary print("Resultant dictionary after assigning minimum value keys:\n", resultantDict)
輸出
Input dictionary 1: {'hello': 2, 'tutorialspoint': 10, 'users': 3} Input dictionary 2: {'hello': 6, 'tutorialspoint': 5, 'users': 7} Resultant dictionary after assigning minimum value keys: {'hello': 2, 'tutorialspoint': 5, 'users': 3}
方法2:使用dict()、min()和zip()函式
**dict()**函式(轉換為字典)
**zip()**函式可用於組合兩個列表/迭代器。
zip(*iterables)
演算法(步驟)
以下是執行所需任務應遵循的演算法/步驟
使用zip函式以組合方式迭代兩個字典。
使用min()函式查詢鍵的最小值。
分配最小值鍵後,列印結果字典。
示例
以下程式使用dict()、min()和zip()函式返回分配最小值鍵後的字典
# input dictionary 1 inputDict_1 = {"hello": 2, "tutorialspoint": 10, "users": 3} # input dictionary 2 inputDict_2 = {"hello": 6, "tutorialspoint": 5, "users": 7} # printing input dictionaries print("Input dictionary 1: ", inputDict_1) print("Input dictionary 2: ", inputDict_2) # Iterate in both dictionaries by passing them in the zip() function. # Get the minimum value of these both dictionaries using the min() function resultantDict = dict([min(i, j) for i, j in zip( inputDict_1.items(), inputDict_2.items())]) # printing resultant dictionary print("Resultant dictionary after assigning minimum value keys:\n", resultantDict)
輸出
Input dictionary 1: {'hello': 2, 'tutorialspoint': 10, 'users': 3} Input dictionary 2: {'hello': 6, 'tutorialspoint': 5, 'users': 7} Resultant dictionary after assigning minimum value keys: {'hello': 2, 'tutorialspoint': 5, 'users': 3}
結論
在本文中,我們學習了兩種不同的最小值鍵賦值方法。此外,我們學習瞭如何使用zip函式組合遍歷兩個字典或可迭代物件。最後,我們學習瞭如何應用一些字典操作並使用字典推導式遍歷字典。