I have list of A, and I want to count average on it's field a.
What's the best way to do it?
class A
{
int a;
int b;
}
void f()
{
var L = new List<A>();
for (int i=0; i<3; i++)
{
L.Add(new A(){a = i});
}
}
Summary: The formula to calculate average is done by calculating the sum of the numbers in the list divided by the count of numbers in the list.
Use the Linq Average() method to find the average of a sequence of numeric values. Firstly, set a sequence. List<int> list = new List<int> { 5, 8, 13, 35, 67 }; Now, use the Queryable Average() method to get the average.
Python doesn't have a built-in function to calculate an average of a list, but you can use the sum() and len() functions to calculate an average of a list. In order to do this, you first calculate the sum of a list and then divide it by the length of that list.
Enumerable.Average
has an overload that takes a Func<T, int>
as an argument.
using System.Linq;
list.Average(item => item.a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With