通过python以特定方式计算嵌套数组的平均值?
calculating the average of nested array in particular manner by python?
我需要一种方法来按照以下方式计算嵌套数组(偶数或奇数)的平均值:
假设我们有这个数组(列表)(甚至 4*4):
mylist = [
[1,6,5,6],
[2,5,6,8],
[7,2,8,1],
[4,4,7,3]
]
输出必须是这样的
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)
但是在那个问题中..我遇到了如何跳转到下一个数组然后返回到第一个数组等等的问题....
**
notice: it has to be a square array ( row length = column length )
**
我提出一个框架挑战:不是一次计算每个象限,而是一次计算所有象限。
假设我们一直在计算四个数字(每个象限的和,如果大小不相等则偏向左上象限),我们可以简单地提前准备好,然后扫描每个单元格矩阵,确定它在哪个象限,然后增加总和:
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
测试用例:
>>> 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]]
好的,这是我的尝试。它有点冗长,但我认为它很容易理解。
# helper func to split nested list (NxN matrix) into 4 quadrants
def split_mat(mat):
n = len(mat)
s = math.ceil(n/2)
up_left = [mat[i][j] for i in range(0, s) for j in range(0, s)]
up_right = [mat[i][j] for i in range(0, s) for j in range(s, n)]
bt_left = [mat[i][j] for i in range(s, n) for j in range(0, s)]
bt_right = [mat[i][j] for i in range(s, n) for j in range(s, n)]
return [up_left, up_right, bt_left, bt_right]
# then the averages you want to calculate becomes trivial
def avg_mat(mat):
quadrants = split_mat(mat)
avgs = [sum(q)//len(q) for q in quadrants]
return [[avgs[0], avgs[1]], [avgs[2], avgs[3]]]
even_list = [
[1,6,5,6],
[2,5,6,8],
[7,2,8,1],
[4,4,7,3]]
print(avg_mat(even_list))
--------------------------------------------------
[[3, 6], [4, 4]]
odd_list = [
[7,9,1],
[4,2,1],
[3,2,3]]
print(avg_mat(odd_list))
--------------------------------------------------
[[5, 1], [2, 3]]
有点高尔夫球,抱歉,输入 phone 表示简称 :)。我利用整数除法来简化逻辑,并在中间列表上使用总计加除法。
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]]
我需要一种方法来按照以下方式计算嵌套数组(偶数或奇数)的平均值:
假设我们有这个数组(列表)(甚至 4*4):
mylist = [
[1,6,5,6],
[2,5,6,8],
[7,2,8,1],
[4,4,7,3]
]
输出必须是这样的
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)
但是在那个问题中..我遇到了如何跳转到下一个数组然后返回到第一个数组等等的问题....
**
notice: it has to be a square array ( row length = column length )
**
我提出一个框架挑战:不是一次计算每个象限,而是一次计算所有象限。
假设我们一直在计算四个数字(每个象限的和,如果大小不相等则偏向左上象限),我们可以简单地提前准备好,然后扫描每个单元格矩阵,确定它在哪个象限,然后增加总和:
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
测试用例:
>>> 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]]
好的,这是我的尝试。它有点冗长,但我认为它很容易理解。
# helper func to split nested list (NxN matrix) into 4 quadrants
def split_mat(mat):
n = len(mat)
s = math.ceil(n/2)
up_left = [mat[i][j] for i in range(0, s) for j in range(0, s)]
up_right = [mat[i][j] for i in range(0, s) for j in range(s, n)]
bt_left = [mat[i][j] for i in range(s, n) for j in range(0, s)]
bt_right = [mat[i][j] for i in range(s, n) for j in range(s, n)]
return [up_left, up_right, bt_left, bt_right]
# then the averages you want to calculate becomes trivial
def avg_mat(mat):
quadrants = split_mat(mat)
avgs = [sum(q)//len(q) for q in quadrants]
return [[avgs[0], avgs[1]], [avgs[2], avgs[3]]]
even_list = [
[1,6,5,6],
[2,5,6,8],
[7,2,8,1],
[4,4,7,3]]
print(avg_mat(even_list))
--------------------------------------------------
[[3, 6], [4, 4]]
odd_list = [
[7,9,1],
[4,2,1],
[3,2,3]]
print(avg_mat(odd_list))
--------------------------------------------------
[[5, 1], [2, 3]]
有点高尔夫球,抱歉,输入 phone 表示简称 :)。我利用整数除法来简化逻辑,并在中间列表上使用总计加除法。
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]]