如何获取列表列表中的第一个索引

如何从我的列表列表中获取第一个索引并获取它的平均值。目前我的清单上有返回值:


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

这是我的预期结果:


var index0 = 2.44

var index1 = 3.44

var index2 = 4.44

仅在单个列表中,我使用它来获取平均值:


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

任何建议/评论 TIA。


冉冉说
浏览 101回答 4
4回答

饮歌长啸

编辑解决方案,因为您进行了编辑。                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());这是你所期望的。希望这项工作。

白衣非少年

看来你需要avg基于列索引而不是行然后.Zip将是你的一个选择,假设您的字符串列表列表是,var myList = new List<List<string>>{&nbsp; &nbsp; &nbsp; &nbsp;new List<string> { "1.11, 2.11, 3.11" },&nbsp; //<= Notice here single item in list with comma(,) separated&nbsp; &nbsp; &nbsp; &nbsp;new List<string> { "2.11, 3.11, 4.11" },&nbsp; &nbsp; &nbsp; &nbsp;new List<string> { "4.11, 5.11, 6.11" }};因此,您需要首先使用逗号 ( ,) 将内部列表中的字符串拆分,以将每个项目作为单独的字符串,var list = myList&nbsp; &nbsp; .SelectMany(x => x&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Select(y => y.Split(',')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Select(z => z.Trim())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.ToList()))&nbsp; &nbsp; .ToList();然后你可以.Zip通过var results = list[0]&nbsp; &nbsp; .Zip(list[1], (a, b) => double.Parse(a) + double.Parse(b))&nbsp; &nbsp; .Zip(list[2], (x, y) => (x + double.Parse(y)) / 3)&nbsp; &nbsp; .ToList();//------------------Print the result---------------foreach (var item in results){&nbsp; &nbsp; Console.WriteLine(Math.Round(item, 2));}如何.Zip运作?让我们获取索引 0 上的所有列值。a = "1.11"b = "2.11"那么第一个Zip结果将是=>double.Parse(a) + double.Parse(b)&nbsp;= 1.11 + 2.11&nbsp;= 3.22所以对于第二个.Zip,x现在是上面第一个的结果,.Zip这意味着x = 3.22y = "4.11"那么第二个Zip结果将是=>(x + double.Parse(y)) / 3&nbsp;= (3.22 + 4.11) / 3&nbsp;= 2.44因此,Average对于您在列索引 0 => 处的值2.44在上面,list[0]:字符串列表中的第一个列表。list[1]:字符串列表中的第二个列表。list[2]:字符串列表中的第三个列表。输出:(来自控制台)

红糖糍粑

您可以压缩所有三个列表using System;using System.Collections.Generic;using System.Linq;public static class MyFunkyExtensions{&nbsp; &nbsp; public static IEnumerable<TResult> ZipThree<T1, T2, T3, TResult>(&nbsp; &nbsp; &nbsp; &nbsp; this IEnumerable<T1> source,&nbsp; &nbsp; &nbsp; &nbsp; IEnumerable<T2> second,&nbsp; &nbsp; &nbsp; &nbsp; IEnumerable<T3> third,&nbsp; &nbsp; &nbsp; &nbsp; Func<T1, T2, T3, TResult> func)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; using (var e1 = source.GetEnumerator())&nbsp; &nbsp; &nbsp; &nbsp; using (var e2 = second.GetEnumerator())&nbsp; &nbsp; &nbsp; &nbsp; using (var e3 = third.GetEnumerator())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return func(e1.Current, e2.Current, e3.Current);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}class MainClass {&nbsp; public static void Main (string[] args) {&nbsp; &nbsp; var list = new List<List<double>> { new List<double> {1.11,2.11,3.11},&nbsp; new List<double>&nbsp; {2.11,3.11,4.11},&nbsp; new List<double>&nbsp; {4.11,5.11,6.11} };&nbsp; &nbsp; var a = list[0].ZipThree(list[1], list[2], (x, y, z) => (x + y + z) / 3);&nbsp; &nbsp; Console.WriteLine(&nbsp; &nbsp; &nbsp; string.Join(",",&nbsp; &nbsp; &nbsp; a.Select(s => s.ToString())));&nbsp; }}它返回2.44333, 3.443333, 4.44333

弑天下

我假设所有内部列表都具有相同的长度,并且您想计算相应索引的平均值(即 index0 是每个列表中第 0 个值的平均值,index1 是第 1 个值的平均值等)。在这种情况下,要获得每个索引的平均值,您可以使用以下代码:int listLength = myList.First().Length; // Length for an array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Count for a List<T>var averages = Enumerable.Range(0, listLength).Select(&nbsp; &nbsp; index => myList.Average(innerList => double.Parse(innerList[index]))&nbsp; &nbsp; ).ToList();
打开App,查看更多内容
随时随地看视频慕课网APP