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

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

问题描述

我有一个包含双精度的 csv 字符串(例如0.3,0.4,0.3"),并且我希望能够输出包含这些数字的累积总和的双数组(例如 [0.3,0.7,1.0]).

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]).

到目前为止,我有

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天全站免登陆