<i id='yq9F3'><tr id='yq9F3'><dt id='yq9F3'><q id='yq9F3'><span id='yq9F3'><b id='yq9F3'><form id='yq9F3'><ins id='yq9F3'></ins><ul id='yq9F3'></ul><sub id='yq9F3'></sub></form><legend id='yq9F3'></legend><bdo id='yq9F3'><pre id='yq9F3'><center id='yq9F3'></center></pre></bdo></b><th id='yq9F3'></th></span></q></dt></tr></i><div id='yq9F3'><tfoot id='yq9F3'></tfoot><dl id='yq9F3'><fieldset id='yq9F3'></fieldset></dl></div>
  • <legend id='yq9F3'><style id='yq9F3'><dir id='yq9F3'><q id='yq9F3'></q></dir></style></legend>

      • <bdo id='yq9F3'></bdo><ul id='yq9F3'></ul>

      <tfoot id='yq9F3'></tfoot>
      1. <small id='yq9F3'></small><noframes id='yq9F3'>

      2. 对二维数组的列求和

        时间:2023-06-05

            <tbody id='fy0Pv'></tbody>
          <tfoot id='fy0Pv'></tfoot>
            • <legend id='fy0Pv'><style id='fy0Pv'><dir id='fy0Pv'><q id='fy0Pv'></q></dir></style></legend>

              • <i id='fy0Pv'><tr id='fy0Pv'><dt id='fy0Pv'><q id='fy0Pv'><span id='fy0Pv'><b id='fy0Pv'><form id='fy0Pv'><ins id='fy0Pv'></ins><ul id='fy0Pv'></ul><sub id='fy0Pv'></sub></form><legend id='fy0Pv'></legend><bdo id='fy0Pv'><pre id='fy0Pv'><center id='fy0Pv'></center></pre></bdo></b><th id='fy0Pv'></th></span></q></dt></tr></i><div id='fy0Pv'><tfoot id='fy0Pv'></tfoot><dl id='fy0Pv'><fieldset id='fy0Pv'></fieldset></dl></div>
                • <bdo id='fy0Pv'></bdo><ul id='fy0Pv'></ul>

                  <small id='fy0Pv'></small><noframes id='fy0Pv'>

                  本文介绍了对二维数组的列求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个 IEnumerable<IEnumerable<double>> 它基本上可以被认为是一个二维数组,我想做的是得到一个一维数组(或列表或其他)这是我原始收藏的所有列的总数.换句话说,如果我有:

                  I have an IEnumerable<IEnumerable<double>> which can basically be thought of as a 2D array of doubles and what I want to do is get a 1D array (or list or whatever) that is the total for all the columns of my original collection. In other words, if I had:

                  1   2   3   4
                  2   3   1   6
                  3   4   5   6
                  -------------
                  6   9   9   16
                  

                  我想要最后一行(显然,这不是原始集合的一部分).

                  I'd want the last line (which is not part of the original collection, obviously).

                  有没有一种简单的方法可以使用 LINQ 来完成此操作,这样我就不必遍历整个事情了?我知道我可以使用 .Sum 合计一行或一列,但我想合计每一行.

                  Is there a simple way to do this with LINQ so that I don't have to loop over the whole thing? I know I can use .Sum to total one row or one column, but I want to total each row.

                  我看到了一些建议使用 group 的其他问题(主要是处理数据库查询),但这似乎对我不起作用.我试过这个:

                  I've seen some other questions (mostly dealing with database queries) that suggest using group but that didn't seem to work for me. I tried this:

                              var totals = from p in toTotal
                                           group p by 1 into g
                                           select g.Sum(t => t.First()); 
                  

                  但这只是全部.

                  有没有聪明的方法来做到这一点?

                  Is there a clever way to do this?

                  例如,如果 toTotal 被定义为:

                  for example, if toTotal was defined as:

                      List<List<double>> toTotal = new List<List<double>>() {
                          new List<double> {1,   2,   3,   4},
                          new List<double> {2,   3 ,  1,   6},
                          new List<double> {3 ,  4,   5 ,  6}
                      };
                  

                  推荐答案

                  好的.我想我已经成功了:

                  Ok. I think I've hit on it:

                  var totals = toTotal.Aggregate((prev, next) => prev.Zip(next, (a, b) => a + b));
                  

                  诀窍是我一直在寻找 Fold(或 Reduce)函数,但在 LINQ 中它被称为 Aggregate.

                  The trick was that I was looking for a Fold (or Reduce) function, but in LINQ it's called Aggregate.

                  这里有一个 fiddle 显示了这个和@Habib 的总行代码(用于比较).与我在这里(以及我在实际代码中使用的)的唯一变化是我需要 .ToList .Zip 的结果,因为对于这个测试用例我有一个 List<List<double>> 如果您没有明确转换为列表,这似乎会让编译器感到不安.在我的实际代码中, toTotal 是一个 IEnumerable 并且不需要转换.

                  Here's a fiddle showing this and @Habib's code for totaling rows (for comparison). The only change from what I have here (and what I was using in my actual code) is that I needed to .ToList the result of .Zip because for this test case I have a List<List<double>> and that seems to upset the compiler if you don't explicitly convert to a list. In my actual code toTotal is an IEnumerable<IEnumerable<double>> and doesn't need converting.

                  这篇关于对二维数组的列求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  1. <small id='83PkY'></small><noframes id='83PkY'>

                    <i id='83PkY'><tr id='83PkY'><dt id='83PkY'><q id='83PkY'><span id='83PkY'><b id='83PkY'><form id='83PkY'><ins id='83PkY'></ins><ul id='83PkY'></ul><sub id='83PkY'></sub></form><legend id='83PkY'></legend><bdo id='83PkY'><pre id='83PkY'><center id='83PkY'></center></pre></bdo></b><th id='83PkY'></th></span></q></dt></tr></i><div id='83PkY'><tfoot id='83PkY'></tfoot><dl id='83PkY'><fieldset id='83PkY'></fieldset></dl></div>
                    <legend id='83PkY'><style id='83PkY'><dir id='83PkY'><q id='83PkY'></q></dir></style></legend>
                      <tbody id='83PkY'></tbody>

                      1. <tfoot id='83PkY'></tfoot>
                          <bdo id='83PkY'></bdo><ul id='83PkY'></ul>