用python以特定方式计算嵌套数组的平均值?

用python以特定方式计算嵌套数组的平均值?,python,arrays,square,Python,Arrays,Square,我需要一种方法来计算嵌套数组(偶数或奇数)的平均值,方法如下: 假设我们有这个数组(列表)(偶数4*4): 输出必须是这样的 mylist = [[3,6], [4,4] ] 根据这个计算, 1 + 6 + 2 + 5 / 4 = 3 5 + 6 + 6 + 8 / 4 = 6 7 + 2 + 4 + 4 / 4 = 4 8 + 1 + 7 + 3 / 4 = 4 如果我们有这样一个奇数嵌套数组,也是一样的 mylist = [[7,9,1], [4,2,1], [3,2,3]] 产出将是

我需要一种方法来计算嵌套数组(偶数或奇数)的平均值,方法如下:

假设我们有这个数组(列表)(偶数4*4):

输出必须是这样的

mylist = [[3,6],
[4,4]
]
根据这个计算,

1 + 6 + 2 + 5 / 4 = 3
5 + 6 + 6 + 8 / 4 = 6
7 + 2 + 4 + 4 / 4 = 4
8 + 1 + 7 + 3 / 4 = 4
如果我们有这样一个奇数嵌套数组,也是一样的

mylist = [[7,9,1],
[4,2,1],
[3,2,3]]
产出将是:

mylist = [[5,1],
[2,3]
]
基于上述相同的计算

7 + 9 + 4 + 2 / 4 = 5
1 + 1 / 2 = 1
3 + 2 / 2 = 2
3 / 1 = 3
那么我们如何用python实现这个过程,请注意,我知道如何对每个数组进行正常平均,就像是逐行增加每个数组的数字,然后除以它的计数

 mylist = [[70,80,90],
[30,40,50],
[0,10,20],
[10,40,40]]
avglist = []
for x in mylist:
    temp = 0
    counter = 0
    for y in x:     
        temp = temp + y
        counter = counter + 1
    avglist.append(temp/counter)
    print()
print()
print(avglist)
但是在这个问题上。。我面临一个问题,如何跳转到下一个数组,然后返回到第一个数组,等等

**

注意:它必须是一个正方形数组(行长度=列长度)


**

我提出了一个帧挑战:不是一次计算每个象限,而是一次计算所有象限

假设我们总是在计算四个数字(每个象限的总和,如果大小不相等,则偏向左上象限),我们只需提前准备它们,然后扫描矩阵的每个单元格,确定它位于哪个象限,并增加总和:

def calculation(matrix):
    # prepare empty lists for each quadrant
    # we will add each element from the matrix to the list that corresponds to its quadrant
    # then at the end we will take the average of each.
    sums = {
        (True, True):   [],  # top-left
        (True, False):  [],  # top-right
        (False, True):  [],  # bottom-left
        (False, False): [],  # bottom-right
    }
    # scan over each cell by row and column index
    for row in range(len(matrix)):
        for col in range(len(matrix[row])):
            # use boolean checks to index into sums depending on quadrant
            sums[(row < len(matrix) / 2,      # is it in the top half?
                  col < len(matrix[row]) / 2  # is it in the left half?
                )].append(matrix[row][col])
    # calculate and return averages (using integer division instead of true division)
    return [[sum(sums[True, True])   // len(sums[True, True]),     # top-left
             sum(sums[True, False])  // len(sums[True, False])],   # top-right
            [sum(sums[False, True])  // len(sums[False, True]),    # bottom-left
             sum(sums[False, False]) // len(sums[False, False])]]  # bottom-right

好吧,这是我的尝试。这有点冗长,但我认为很容易理解

#helper func将嵌套列表(NxN矩阵)拆分为4个象限
def分离垫(垫):
n=透镜(mat)
s=数学单元(n/2)
左上=[mat[i][j]表示范围(0,s)内的i,表示范围(0,s)内的j]
上_right=[mat[i][j]表示范围(0,s)中的i,表示范围(s,n)中的j]
bt_left=[mat[i][j]表示范围(s,n)中的i,表示范围(0,s)中的j]
bt_right=[mat[i][j]表示范围(s,n)中的i,表示范围(s,n)中的j]
返回[左上、右上、左下、右下]
#然后,要计算的平均值就变得微不足道了
def平均值垫(垫):
象限=分割垫(垫)
平均值=[sum(q)//len(q)表示象限中的q]
返回[[avgs[0]、avgs[1]、[avgs[2]、avgs[3]]
偶数列表=[
[1,6,5,6],
[2,5,6,8],
[7,2,8,1],
[4,4,7,3]]
打印(平均材料(偶数列表))
--------------------------------------------------
[[3, 6], [4, 4]]
odd_列表=[
[7,9,1],
[4,2,1],
[3,2,3]]
打印(平均材料(奇数列表))
--------------------------------------------------
[[5, 1], [2, 3]]

有点高尔基,对不起,在手机上输入意味着短名字:)。我利用整数除法简化逻辑,并在中间列表上使用总计加除法

from itertools import product

def quad_avgs(M):
  sums = [[0,0],[0,0]]
  L, S, s = len(M), len(M) / 2, len(M) - len(M) / 2
  for r, c in product(range(L), repeat=2):
      sums[r / S][c / S] += M[r][c]
  return [[sums[r][c] / ([S, s][r] * [S, s][c]) for c in [0, 1]] for r in [0, 1]]

你能使用numpy吗?或者使用列表是一种约束?它必须是一个列表…@Piss所有的问题都在算法本身而不是平均值上。。谢谢你的帮助replying@HussamGold在您的第一个偶数4x4示例中,计算
1+6+5+6/4=3
是否假定为
(1+6+2+5)/4=3
?另外,我要澄清的是,你实际上并不是在计算平均数,而是在计算平均数的下限,对吗?@JethroCao u r right抱歉弄错了。。是的,从十进制数变成3
>>> mylist = [
... [1,6,5,6],
... [2,5,6,8],
... [7,2,8,1],
... [4,4,7,3]
... ] 
>>> calculation(mylist)
[[3, 6], [4, 4]]

>>> mylist = [[7,9,1],
... [4,2,1],
... [3,2,3]]
>>> calculation(mylist)
[[5, 1], [2, 3]]
from itertools import product

def quad_avgs(M):
  sums = [[0,0],[0,0]]
  L, S, s = len(M), len(M) / 2, len(M) - len(M) / 2
  for r, c in product(range(L), repeat=2):
      sums[r / S][c / S] += M[r][c]
  return [[sums[r][c] / ([S, s][r] * [S, s][c]) for c in [0, 1]] for r in [0, 1]]