LINQ运算符,以基于通用定界符的双重列表的拆分列表[英] LINQ operator to split List of doubles to multiple list of double based on generic delimiter

本文是小编为大家收集整理的关于LINQ运算符,以基于通用定界符的双重列表的拆分列表的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

i具有List双重值,其中某些元素是NAN.我想转换此列表 使用NAN作为定界符列入多个双打列表. 示例:

List<double> values =  new List<double>        
   { 1.2, 2.2, 3.2, Double.NaN, 2.2, 2.3, Double.NaN, 4.1, 4.2, 4.3 }; 
List<List<double>> result = values.SomeLinqOperator....

推荐答案

List<List<double>> result = values.GroupDelimited(x => x == double.NaN)
                                  .Select(g => g.ToList())
                                  .ToList();

使用使用 groupDelimited扩展方法

其他推荐答案

什么也没有内置,但是您可以写一个:

public static IEnumerable<List<T>> SplitBy<T>(
    this IEnumerable<T> source, T delimiter)
{
    List<T> buffer = new List<T>();
    var comparer = EqualityComparer<T>.Default;
    foreach (var item in source)
    {
        if (comparer.Equals(item, delimiter))
        {
            yield return buffer;
            buffer = new List<T>();
        }
        else
        {
            buffer.Add(item);
        }
    }
    yield return buffer;
}
static void Main()
{
    List<double> values = new List<double> 
        {1.2,2.2,3.2,double.NaN,2.2,2.3,double.NaN,4.1,4.2,4.3 };
    List<List<double>> result = values.SplitBy(double.NaN).ToList();
}

其他推荐答案

如果您在一起有两个double.NaN值,则将包括空列表,或一个在输入的开始或结尾处:

:

List<List<double>> result = new List<List<double>>();
List<double> current = new List<double>();
foreach (double d in values)
{
    if (d == double.NaN)
    {
        result.Add(current);
        current = new List<double>();
    }
    else
    {
        current.Add(d);
    }
}
result.Add(current);

本文地址:https://www.itbaoku.cn/post/1556935.html

问题描述

I have a List double values in which some of the elements are NaN. I want to convert this list into multiple list of doubles using NaN as delimiter. Example:

List<double> values =  new List<double>        
   { 1.2, 2.2, 3.2, Double.NaN, 2.2, 2.3, Double.NaN, 4.1, 4.2, 4.3 }; 
List<List<double>> result = values.SomeLinqOperator....

推荐答案

List<List<double>> result = values.GroupDelimited(x => x == double.NaN)
                                  .Select(g => g.ToList())
                                  .ToList();

using the GroupDelimited Extension Method from here.

其他推荐答案

Nothing built in, but you can write one:

public static IEnumerable<List<T>> SplitBy<T>(
    this IEnumerable<T> source, T delimiter)
{
    List<T> buffer = new List<T>();
    var comparer = EqualityComparer<T>.Default;
    foreach (var item in source)
    {
        if (comparer.Equals(item, delimiter))
        {
            yield return buffer;
            buffer = new List<T>();
        }
        else
        {
            buffer.Add(item);
        }
    }
    yield return buffer;
}
static void Main()
{
    List<double> values = new List<double> 
        {1.2,2.2,3.2,double.NaN,2.2,2.3,double.NaN,4.1,4.2,4.3 };
    List<List<double>> result = values.SplitBy(double.NaN).ToList();
}

其他推荐答案

This will include empty lists if you have two double.NaN values together, or one at the start or end of the input:

List<List<double>> result = new List<List<double>>();
List<double> current = new List<double>();
foreach (double d in values)
{
    if (d == double.NaN)
    {
        result.Add(current);
        current = new List<double>();
    }
    else
    {
        current.Add(d);
    }
}
result.Add(current);