I have a list of lists that is in the following form (akin to a square matrix):
a = [[1,2,3,4,5,6],
[5,6,7,8,3,4],
[9,8,4,6,2,1],
[3,4,5,1,4,5],
[4,3,7,8,1,4],
[3,2,5,6,1,8]]
I would like to get a new list of lists with the average of 4 adjacent values that forms a square, i.e. new_list[0][0]
would be the mean of [1+2+5+6]
, new_list[0][1]
would be the mean of [3+4+7+8]
, new_list[0][2]
would be the mean of [5+6+3+4]
, and so on.
How can I achieve this in a pythonic way?
Thank you very much for any advice!
EDITED:
Thank you for pointing out this answer has been answered before - I didn't formulate my question clear enough it seems. Anyway, adapted from answer to this question, I got the solution:
a = np.array(a)
a_new = np.zeros((3, 3))
for i in range(3):
for j in range(3):
a_new[i][j] = np.mean(a[i*2:2+i*2, j*2:2+j*2])