在Python中查詢列表元素之和與矩陣中缺失元素之和的差值
在本文中,我們將學習如何查詢列表元素之和與矩陣中缺失元素之和的差值。
使用的方法
以下是完成此任務的各種方法:
使用for迴圈和from_iterable()函式
使用sum()和from_iterable()函式
使用Counter()函式
示例
假設我們已經輸入了一個矩陣和一個目標列表。我們現在將找到列表元素之和與矩陣中缺失元素之和的差值。
輸入
inputMatrix = [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, 7, 2]] targetList = [9, 2, 1, 10, 3, 1]
輸出
The absolute difference between matrix sum and list sum: 36
解釋
在這個例子中:
列表中缺失的矩陣元素是(9),所以和是9。
矩陣中缺失的列表元素是(6,5,4,8,15,7),所以和是45
矩陣和與列表和的絕對差值 = 45-9= 36
方法1:使用for迴圈和from_iterable()函式
itertools.chain.from_iterable()函式
它屬於終止迭代器類別。
from_iterable() 函式返回一個扁平化的可迭代物件,其中包含輸入可迭代物件的所有元素,並且只接受單個可迭代物件作為引數。輸入可迭代物件的所有元素也應該是可迭代的。
語法
chain.from_iterable(iterable)
示例
下面的程式使用for迴圈和from_iterable()函式返回輸入矩陣中缺失的列表元素之和與反之的差值:
# importing chain from itertools module
from itertools import chain
# input matrix
inputMatrix = [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, 7, 2]]
# printing input matrix
print("Input Matrix: ", inputMatrix)
# input target list
targetList = [9, 2, 1, 10, 3, 1]
# getting the flattened Matrix
flattenMatrix = list(chain.from_iterable(inputMatrix))
# storing the sum of target list elements that are not in the flattened matrix
listSum = 0
# travsering in the target list
for i in targetList:
# checking whether the current element is not in a flattened matrix
if i not in flattenMatrix:
# adding that element to the list sum variable if the condition is true
listSum += i
# storing the sum of matrix elements that are not in the target list
matrixSum = 0
# travsering in the flattened matrix
for i in flattenMatrix:
# checking whether the current element is not in the target list
if i not in targetList:
# adding that element to matrix sum variable if the condition is true
matrixSum += i
# getting the absolute difference between matrix sum and list sum
resultantDiff = abs(matrixSum - listSum)
# printing the resultant difference
print("Absolute difference between matrix sum and list sum:", resultantDiff)
輸出
執行後,上述程式將生成以下輸出:
Input Matrix: [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, 7, 2]] Absolute difference between matrix sum and list sum: 36
方法2:使用sum()和from_iterable()函式
sum()函式 - 返回可迭代物件中所有專案的總和。
示例
下面的程式使用sum()、from_iterable()函式返回輸入矩陣中缺失的列表元素之和與反之的差值:
# importing chain from itertools module
from itertools import chain
inputMatrix = [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, 7, 2]]
print("Input Matrix: ", inputMatrix)
targetList = [9, 2, 1, 10, 3, 1]
# getting the flattened matrix of the input matrix
flattenMatrix = list(chain.from_iterable(inputMatrix))
# getting the sum of target list elements that are not in the flattened Matrix
listSum = sum([i for i in targetList if i not in flattenMatrix])
# getting the sum of input matrix elements that are not in the target list
matrixSum = sum([i for i in flattenMatrix if i not in targetList])
# getting the absolute difference between matrix sum and list sum
resultantDiff = abs(matrixSum - listSum)
print("Absolute difference between matrix sum and list sum:", resultantDiff)
輸出
Input Matrix: [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, 7, 2]] Absolute difference between matrix sum and list sum: 36
方法3:使用Counter()函式
Counter()函式 - 一個子類,用於計算可雜湊物件。它在被呼叫/呼叫時隱式地建立一個可迭代物件的雜湊表。
示例
下面的程式使用Counter()函式返回輸入矩陣中缺失的列表元素之和與反之的差值:
# importing Counter from the collections module
from collections import Counter
inputMatrix = [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, 7, 2]]
targetList = [9, 2, 1, 10, 3, 1]
# empty list for storing flattened matrix
flattenMatrix = []
# traversing through the input matrix and getting its flattened matrix
for p in inputMatrix:
for q in p:
flattenMatrix.append(q)
# getting the frequency of the flattened matrix
matrixFreq = Counter(flattenMatrix)
# getting the frequency of the target list
listFreq = Counter(targetList)
# storing the sum of target list elements that are not in the flattened matrix
listSum = []
# traversing through the target list
for p in targetList:
# checking whether the current element is not in a matrix frequency list
if p not in matrixFreq:
# appending that element to the list sum if the condition is true
listSum.append(p)
# storing the sum of matrix elements that are not in the target list
matrixSum = []
# travsering in the flattened matrix
for p in flattenMatrix:
# checking whether the current element is not in a list frequency list
if p not in listFreq:
# appending that element to the matrix sum
matrixSum.append(p)
# getting the absolute difference between matrix sum and list sum
resultantDiff = abs(sum(matrixSum) - sum(listSum))
print("Absolute difference between matrix sum and list sum:", resultantDiff)
輸出
Absolute difference between matrix sum and list sum: 36
結論
本文介紹了三種不同的方法來確定矩陣中缺失的列表元素總數與反之的差值。我們還學習瞭如何使用chain.from_iterable()函式來展平矩陣或列表。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP