Python 找到矩阵和列表元素总和的差异

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程