-1

How can I get the first indexes from my list of lists and get it's average value. Currently I have return value on my list:

[["1.11, 2.11, 3.11"], ["2.11, 3.11, 4.11"], ["4.11, 5.11, 6.11"]]

Here is my expected result:

var index0 = 2.44
var index1 = 3.44
var index2 = 4.44

On single list only I am using this to get the average:

var avg = myList.Select(double.Parse).Average();

Any suggestion/comments TIA.

Syntax Rommel
  • 932
  • 2
  • 16
  • 40

5 Answers5

1

Edit Solution because you edited.

                String[][] TValue = new String[][]
                {
                    new String[] {"1.11", "2.11", "3.11" },
                    new String[] {"2.11", "3.11", "4.11" }, 
                    new String[] {"4.11", "5.11", "6.11" }
                };

                Console.WriteLine("Avg[0] => {0:F2}", TValue.Select(x => double.Parse(x[0])).Average());
                Console.WriteLine("Avg[1] => {0:F2}", TValue.Select(x => double.Parse(x[1])).Average());
                Console.WriteLine("Avg[2] => {0:F2}", TValue.Select(x => double.Parse(x[2])).Average());

this is what you expected.

hope this work.

Arphile
  • 841
  • 6
  • 18
0

You could do so using Linq.

Updated based on comment

var list = new [] {  "1.11, 2.11, 3.11" , "2.11,3.11, 4.11" ,  "4.11,5.11,6.11" };
var collection = list.Select(x => x.Split(',').Select(c => double.Parse(c)).ToList()).ToList();
var result = collection.First()
               .Select((dummy, i) =>
                  collection.Average(inner => inner[i]));

Output

2.44
3.44
4.44
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
0

You can zip all three lists

using zipThree from How to combine more than two generic lists in C# Zip?

using System;
using System.Collections.Generic;
using System.Linq;

public static class MyFunkyExtensions
{
    public static IEnumerable<TResult> ZipThree<T1, T2, T3, TResult>(
        this IEnumerable<T1> source,
        IEnumerable<T2> second,
        IEnumerable<T3> third,
        Func<T1, T2, T3, TResult> func)
    {
        using (var e1 = source.GetEnumerator())
        using (var e2 = second.GetEnumerator())
        using (var e3 = third.GetEnumerator())
        {
            while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext())
                yield return func(e1.Current, e2.Current, e3.Current);
        }
    }
}

class MainClass {
  public static void Main (string[] args) {
    var list = new List<List<double>> { new List<double> {1.11,2.11,3.11},  new List<double>  {2.11,3.11,4.11},  new List<double>  {4.11,5.11,6.11} };

    var a = list[0].ZipThree(list[1], list[2], (x, y, z) => (x + y + z) / 3);
    Console.WriteLine(
      string.Join(",",
      a.Select(s => s.ToString())));
  }
}

And it returns

2.44333, 3.443333, 4.44333

jgoday
  • 2,768
  • 1
  • 18
  • 17
0

I'm assuming all the inner lists have the same length, and you want to count the average of the corresponding indices (i.e. index0 is average of 0th value from each list, index1 is average of the 1st value etc.).

In such case to obtain the averages for each index you use the following code:

int listLength = myList.First().Length; // Length for an array
                                        // Count for a List<T>

var averages = Enumerable.Range(0, listLength).Select(
    index => myList.Average(innerList => double.Parse(innerList[index]))
    ).ToList();
Alice
  • 585
  • 5
  • 16
0

It seems that you need to get avg based on columns index instead of rows then .Zip will be one option for you,

Suppose your list of list of string is,

var myList = new List<List<string>>
{
       new List<string> { "1.11, 2.11, 3.11" },  //<= Notice here single item in list with comma(,) separated
       new List<string> { "2.11, 3.11, 4.11" },
       new List<string> { "4.11, 5.11, 6.11" }
};

So you need to first split your string in inner list with comma(,) to get each item as separate string,

var list = myList
    .SelectMany(x => x
           .Select(y => y.Split(',')
           .Select(z => z.Trim())
           .ToList()))
    .ToList();

Then you can make .Zip on all above 3 list by

var results = list[0]
    .Zip(list[1], (a, b) => double.Parse(a) + double.Parse(b))
    .Zip(list[2], (x, y) => (x + double.Parse(y)) / 3)
    .ToList();

//------------------Print the result---------------

foreach (var item in results)
{
    Console.WriteLine(Math.Round(item, 2));
}

How .Zip works?

Lets take all columns value on index 0.

a = "1.11"
b = "2.11"

Then 1st Zip result will be =>

double.Parse(a) + double.Parse(b) 
= 1.11 + 2.11 
= 3.22

So for 2nd .Zip the x now be the result of the above 1st .Zip that means

x = 3.22
y = "4.11"

Then 2nd Zip result will be =>

(x + double.Parse(y)) / 3 
= (3.22 + 4.11) / 3 
= 2.44

So for Average of your values at column index 0 => 2.44

In above,

  • list[0] : 1st list in your list of list of string.
  • list[1] : 2nd list in your list of list of string.
  • list[2] : 3rd list in your list of list of string.

Output: (From Console)

enter image description here

er-sho
  • 9,581
  • 2
  • 13
  • 26