4 回答

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個贊
編輯解決方案,因?yàn)槟M(jìn)行了編輯。
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());
這是你所期望的。
希望這項(xiàng)工作。

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個贊
看來你需要avg基于列索引而不是行然后.Zip將是你的一個選擇,
假設(shè)您的字符串列表列表是,
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" }
};
因此,您需要首先使用逗號 ( ,) 將內(nèi)部列表中的字符串拆分,以將每個項(xiàng)目作為單獨(dú)的字符串,
var list = myList
.SelectMany(x => x
.Select(y => y.Split(',')
.Select(z => z.Trim())
.ToList()))
.ToList();
然后你可以.Zip通過
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));
}
如何.Zip運(yùn)作?
讓我們獲取索引 0 上的所有列值。
a = "1.11"
b = "2.11"
那么第一個Zip結(jié)果將是=>
double.Parse(a) + double.Parse(b)
= 1.11 + 2.11
= 3.22
所以對于第二個.Zip,x現(xiàn)在是上面第一個的結(jié)果,.Zip這意味著
x = 3.22
y = "4.11"
那么第二個Zip結(jié)果將是=>
(x + double.Parse(y)) / 3
= (3.22 + 4.11) / 3
= 2.44
因此,Average對于您在列索引 0 => 處的值2.44
在上面,
list[0]:字符串列表中的第一個列表。
list[1]:字符串列表中的第二個列表。
list[2]:字符串列表中的第三個列表。
輸出:(來自控制臺)

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個贊
您可以壓縮所有三個列表
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())));
}
}
它返回
2.44333, 3.443333, 4.44333

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個贊
我假設(shè)所有內(nèi)部列表都具有相同的長度,并且您想計(jì)算相應(yīng)索引的平均值(即 index0 是每個列表中第 0 個值的平均值,index1 是第 1 個值的平均值等)。
在這種情況下,要獲得每個索引的平均值,您可以使用以下代碼:
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();
- 4 回答
- 0 關(guān)注
- 168 瀏覽
添加回答
舉報