使用LINQ找到数字数组在C#中的累积和 [英] using LINQ to find the cumulative sum of an array of numbers in C#

查看:216
本文介绍了使用LINQ找到数字数组在C#中的累积和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含双打(如0.3,0.4,0.3)的CSV字符串,我想能够输出包含这些数字(如[0.3,0.7,1.0])的累积和双阵列。

到目前为止,我有

双击[] =概率textBox_f.Text.Split(新的char [] {','})选择(S = GT; double.Parse(S))ToArray的();

赋予的编号的数组,而不是数字的累计总和。

有没有办法继续这种前pression得到我想要的东西,或者我需要使用迭代创建数组的新数组我已经有了?


解决方案

  VAR输入=新的双[] {...}
双总和= 0;无功输出=输入
    。选择(W =>总结+ = W);

I have a csv string containing doubles (e.g "0.3,0.4,0.3"), and I want to be able to output a double array containing the cumulative sum of these numbers (e.g [0.3,0.7,1.0]).

So far, I have

double[] probabilities = textBox_f.Text.Split(new char[]{','}).Select(s => double.Parse(s)).ToArray();

which gives the numbers as an array, but not the cumulative sum of the numbers.

Is there any way to continue this expression to get what I want, or do I need to use iteration to create a new array from the array I already have?

解决方案

var input=new double[]{ ... }
double sum=0;

var output=input
    .Select(w=>sum+=w);

这篇关于使用LINQ找到数字数组在C#中的累积和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆