Anurag Dutta
Anurag Dutta

Reputation: 41

How to find the average of each element in two different lists

These are my arrays

image =  [[0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,0],
          [0,0,1,1,1,1,1,1,0,0],
          [0,0,1,1,1,1,1,1,0,0],
          [0,0,1,1,1,1,1,1,0,0],
          [0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,0] 
           ]
image1 = [[0,0,0,0,0,0,0,0,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0] 
           ]

I want to get the average of each element from the different arrays and make a list of new averages, please help

This is what I have done so far: enter image description here

Upvotes: 0

Views: 155

Answers (3)

Mont
Mont

Reputation: 194

Numpy ninja code.

import numpy as np
image =  [[0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,0],
          [0,0,1,1,1,1,1,1,0,0],
          [0,0,1,1,1,1,1,1,0,0],
          [0,0,1,1,1,1,1,1,0,0],
          [0,0,0,0,0,0,0,0,0,0],
          [0,0,0,0,0,0,0,0,0,0] 
           ]
image1 = [[0,0,0,0,0,0,0,0,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0],
          [0,0,1,0,0,0,1,1,0,0] 
           ]
images = [image,image1]
np.array(images).mean(axis=0)

Upvotes: 1

AcK
AcK

Reputation: 2133

In addition to the other answers, your code would have worked as following:

imagi = []
for x, y in zip(img, img1):
    z = (x + y) / 2
    imagi.append(z)

Upvotes: 0

iris_hu
iris_hu

Reputation: 183

import numpy as np
ans = (np.array(image) + np.array(image1))/2.0
ans = ans.tolist()

but it seems to be a duplicate question Average values in two Numpy arrays

Upvotes: 2

Related Questions