在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()函数来展平矩阵或列表。

更新于:2023年1月27日

141 次浏览

启动您的职业生涯

通过完成课程获得认证

开始学习
广告