# How do I get the (average) sum of three lists in C #?

advertisements

I have three lists which each list represents only 0s and 1s which related to the pixel values of three images. My question is how can I get the sum (average) of those three lists and represent it in a new list? here is example of my image1:

``````List<int> image1 = new List<int>();
int blackColor = 0;

for (int x = 0; x < bmp1.Width; x++)
{
for (int y = 0; y < bmp1.Height; y++)
{
Color color = bmp1.GetPixel(x, y);

if (color.ToArgb() == Color.Black.ToArgb())
{
image1.Add(0);
blackColor++;
}
else
{
image1.Add(1);
}
}
}
```
```

Let me makes sure I understand the problem. You have three lists of the same length:

``````list A:  1, 2, 4, 3
list B:  3, 2, 4, 1
List C:  2, 7, 1, 8
```
```

and you wish to get a third list that is the average of each:

``````List D:  2, 4, 3, 4
```
```

Yes?

This is a job for zip join.

``````var sumOfFirstTwo = list1.Zip(list2, (x, y)=>x + y);
```
```

`sumOfFirstTwo` is now the sequence that is the sum of the first two lists.

``````var sumOfAllThree = sumOfFirstTwo.Zip(list3, (x, y)=>x + y);
```
```

`sumOfAllThree` is now the sequence that is the sum of all three lists.

``````var average = sumOfAllThree.Select(x=>x/3).ToList();
```
```

Make sense?