我有List<Tuple<string[], double[]>>
List<Tuple<string[], double[]>> tt = new List<Tuple<string[], double[]>>();
var t1 = new Tuple<string[], double[]>(
new string[] { "java", "c#", "nn" },
new double[] { 2, 3, 0 });
var t2 = new Tuple<string[], double[]>(
new string[] { "java", "c#", "nn" },
new double[] { 0, 3, 1 });
var t3 = new Tuple<string[], double[]>(
new string[] { "java", "c#", "nn" },
new double[] { 2, 1, 0 });
tt.Add(t1);
tt.Add(t2);
tt.Add(t3);
假设我想将double[]
中的每个元素除以列中的总计数非零count
。我会得到以下结果
t1 2/2, 3/3 0/1
t2 0/2, 3/3 1/1
t3 2/2, 1/3 0/1
这是我尝试过的,但它会计算行总数而不是列
var XX = (from tuples in tt
let rows = tuples.Item2.ToArray()
let result = rows.Select(x => x/rows.Count(i => i != 0)).ToArray()
select new Tuple<string[], double[]>(tuples.Item1, result)
).ToList();