3 个 IEnumerable 的平均值

发布于 2025-01-07 10:52:36 字数 890 浏览 4 评论 0原文

我需要平均 3 个 IEnumerable 集合并创建一个新的 IEnumerable,结果如下:

var result1 = GetResult1()//Returns an enumerable collection having elements 1,2,3,4

var result2 = GetResult2()//Returns an enumerable collection having elements 3,4,2,6

var result3 = GetResult3()//returns an enumerable collection having elements 2,5,1,6

//I need to create a collection which has the averages of the above results as below:

var result4 = GetResult4()//Should return 2.00,3.67,2.33,5.33.
  • 每个结果将具有相同数量的元素,即 result1、result2、result3 将具有相同数量的元素(在本例中为 4) ,但实际上它的范围可以达到 1500)。

  • 结果中的每个元素对应于结果的平均值,即在 result4 集合中,第一个元素将是 1,3,2 的平均值,分别对应于 result1,result2,result3 中的第一个元素的平均值

  • 结果集合的小数位应四舍五入到 2。

一个明显的答案是迭代集合中的每个元素并在结果中创建元素手动收集(即通过 for 循环),但是我想避免它,因为结果将在每个集合中包含接近 1500 个元素,并且希望使用 LINQ 进行某种惰性评估。

请您帮我指点一下吗?

干杯, -麦克风

I need to average 3 IEnumerable collection and create a new IEnumerable with the result as follows:

var result1 = GetResult1()//Returns an enumerable collection having elements 1,2,3,4

var result2 = GetResult2()//Returns an enumerable collection having elements 3,4,2,6

var result3 = GetResult3()//returns an enumerable collection having elements 2,5,1,6

//I need to create a collection which has the averages of the above results as below:

var result4 = GetResult4()//Should return 2.00,3.67,2.33,5.33.
  • Each results will be having the same number of elements i.e result1, result2, result3 will be having the same number of elements (in this case 4, but actually it can range till 1500).

  • Each element in the result corresponds to the average value of the results i.e in result4 collection the first element will be an average of 1,3,2 which corresponds to the average of the first element in result1,result2,result3 respectively and hence forth.

  • The resultant collection should be having decimal places rounded to 2.

An obvious answer is to iterate through each element in the collections and create the elements in the resultant collection manually (i.e via a for loop), however I want to avoid it as the results will have close to 1500 elements in each collection and would like to do some sort of lazy evaluation using LINQ.

Please can you help me with some pointers?

Cheers,
-Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街女流氓 2025-01-14 10:52:37

如果您使用的是 .NET 4,则可以在此处使用 Enumerable.Zip 以获得良好效果:

var averages = result1.Zip(result2, (x, y) => x + y)
                      .Zip(result3, (xy, z) => (xy + z) / 3.0d)
                      .ToList();

这不会进行任何舍入 - 这通常更适合格式化 步骤。请注意,无论如何,这些值只能以 0、0.33 或 0.67 结尾,因为您只能除以 3。这里我使用了 double,但另一种选择是使用 decimal< /code> - 这取决于值的真正含义。

If you're using .NET 4, you can use Enumerable.Zip to good effect here:

var averages = result1.Zip(result2, (x, y) => x + y)
                      .Zip(result3, (xy, z) => (xy + z) / 3.0d)
                      .ToList();

This won't do any rounding - that would normally be more appropriate for a formatting step. Note that the values can only end with 0, 0.33, or 0.67 anyway, as you're only dividing by 3. Here I've used double but another option would be to use decimal - it depends on what the values really mean.

榕城若虚 2025-01-14 10:52:37

实现此目的的一种方法是编写一个方法,该方法接受列表的列表,并执行必要的工作。类似于:

 private static IEnumerable<double> AverageOfLists(params IEnumerable<double>[] values)
 {
     var lists = values.Select(v => v.ToList()).ToArray();

     for(var i=0;i<lists[0].Count;i++)
     {
         var sum = 0.0;
         for(var j=0;j<lists.Length;j++)
         {
             sum+= lists[j][i];
         }
         yield return sum/lists.Length;
     }
 }

用法将是

var result4 = AverageOfLists(result1,result2,result3);

实时示例: http://rextester.com/IGOSE98291

注意:不包括四舍五入@Jon 提到的原因相同;舍入是格式化/显示步骤。

One way to achieve this is to write a method which takes a list of lists, and does the necessary work. Something like:

 private static IEnumerable<double> AverageOfLists(params IEnumerable<double>[] values)
 {
     var lists = values.Select(v => v.ToList()).ToArray();

     for(var i=0;i<lists[0].Count;i++)
     {
         var sum = 0.0;
         for(var j=0;j<lists.Length;j++)
         {
             sum+= lists[j][i];
         }
         yield return sum/lists.Length;
     }
 }

Usage would then be

var result4 = AverageOfLists(result1,result2,result3);

Live example: http://rextester.com/IGOSE98291

NB: Not included rounding for the same reason @Jon mentioned; rounding is a formatting/display step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文