简体   繁体   English

如何将列表中的每个元素除以整数? C#

[英]How can I divide each element in a list by an integer? C#

所以我创建了一个包含双精度数的列表,是否可以用整数变量划分该列表中的每个元素?

List<Double> amount = new List<Double>();

Just create a new list with the modified contents: 只需使用修改后的内容创建一个新列表:

var newAmounts = amount.Select(x => x / 10).ToList();

Creating new data is less error-prone than modifying existing data. 与修改现有数据相比,创建新数据更不容易出错。


foreach 的foreach

You can iterate over each item with foreach : 您可以使用foreach迭代每个项目:

foreach(var item in amount)
{
    var result = item / 3;
}

If you want to store the results in a new list you can do it inside the loop... 如果要将结果存储在新列表中,可以在循环内执行...

var newList = new List<double>(amount.Count); //<-- set capacity for performance
foreach(var item in amount)
{
    newList.Add(item / 3);
}

LINQ LINQ

... or use Linq to an IEnumerable<double> : ...或者将Linq用于IEnumerable<double>

var newList = from item in amount select item / 3;

You can also use Linq extension methods: 您还可以使用Linq扩展方法:

var newList = amount.Select(item => item / 3);

Or if you want a List<double> from Linq, you can do it with ToList() : 或者如果你想要一个来自Linq的List<double> ,你可以使用ToList()

var newList = (from item in amount select item / 3).ToList();

... or ... ... 要么 ...

var newList = amount.Select(item => item / 3).ToList();

for 对于

As an alternative you can use a simple for : 作为替代方案,您可以使用简单for

for (int index = 0; index < amount.Count; index++)
{
    var result = amount[index] / 3;
}

This approach will allow you to do the modifications in place: 这种方法将允许您进行适当的修改:

for (int index = 0; index < amount.Count; index++)
{
    amount[index] = amount[index] / 3;
}

PLINQ PLINQ

You may also consider using Parallel LINQ (with AsParallel ): 您也可以考虑使用Parallel LINQ(使用AsParallel ):

var newList = amount.AsParallel().Select(item => item / 3).ToList();

Warning : The result may be out of order. 警告 :结果可能不正常。

This will take advantage of multicore CPU, by running the operations for each item in parallel. 这将利用多核CPU,通过并行运行每个项目的操作。 This is particularly good for large lists, and for operations that are independent for each item. 这对于大型列表以及对每个项目独立的操作尤其有用。


Comparison 对照

  1. foreach : Easy to read and write, easy to remember. foreach :易于阅读和书写,易于记忆。 Also allows for some optimizations. 还允许一些优化。
  2. Linq : Better if you are used to SQL, also allows for lazy execution. Linq :如果你习惯了SQL,那就更好了,也允许延迟执行。
  3. for : Doing the operation in place requires less memory. for :执行操作需要较少的内存。 Allows for a more control. 允许更多控制。
  4. PLinq : All you love from Linq, optimized for multiple cores. PLinq :所有你喜欢的Linq,针对多核心进行了优化。 Although some caution is needed. 虽然需要谨慎一些。

Of course the simple way is to iterate the list and divide each number: 当然,简单的方法是迭代列表并划分每个数字:

foreach(var d in amount) {
    var result = d / 3;
}

You can store the result in a new list. 您可以将结果存储在新列表中。

In case you want to modify the same instance (rather than creating a new collection), do: 如果您想要修改同一个实例(而不是创建新的集合),请执行以下操作:

for (int i = 0; i < amount.Count; ++i)
    amount[i] /= yourInt32Divisor;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM