5

linqを使用して整数のリストから範囲を抽出しようとしています:

たとえば、次のリストを分割しようとしています。

List<int> numberList = new List<int>() { 30, 60, 90, 120, 150, 180, 270, 300, 330 };  

次のような整数範囲のリストに変換します。

{ 30, 180 }
{ 270, 330 }

つまり、次の seq が 30 より大きい場合

もう一つの例 :

List<int> numberList = new List<int>() { 30, 60, 120, 150, 270, 300, 330 };  

次のような整数範囲のリストに変換します。

{ 30, 60 }
{ 120, 150 }
{ 270, 330 }

for ループを使用して可能な限り最善の方法を見つけようとしましたが、これを行うために linq クエリを使用しようとする場所がわかりません。

4

6 に答える 6

3

分割を処理するメソッドを作成できます。

IEnumerable<IList<int>> SplitValues(IList<int> input, int difference = 30)
{
    List<int> results = new List<int>();
    int last = input.First();
    foreach(var value in input)
    {
        if (value - last > difference)
        {
            yield return new[] {results.First(), results.Last()};
            results = new List<int>();
        }

        results.Add(value);
        last = value;
    }

    yield return new[] {results.First(), results.Last()};
}

これは、説明されている仕様と一致し、次を返します。

{ 30, 60 }
{ 120, 150 }
{ 270, 330 }

範囲のないコレクション内の単一の値は複製されることに注意してください。たとえば、次の{ 30, 120, 150 }ように返されます。

{ 30, 30 }
{ 120, 150 }
于 2013-06-04T22:42:18.863 に答える
1

良い。これを行うには多くの方法があり、すべてに長所と短所があります。ここにさらに別の解決策があります。誰かの役に立てば幸いです。

public static IEnumerable<TSource[]> ToRanges<TSource>(
    this IEnumerable<TSource> source, Func<TSource, TSource, TSource, bool> isNear)
{            
    List<TSource[]> result = source./*OrderBy(value => value).*/Aggregate(
        new List<TSource[]> { new[] { source.First(), source.First() } },
        (ranges, currentValue) => {
            TSource[] currentRange = ranges.Last();
            TSource previousValue = currentRange[1];

            if (isNear(currentRange[0], previousValue, currentValue))
                currentRange[1] = currentValue;
            else
                ranges.Add(new[] { currentValue, currentValue});

            return ranges;
        }
    );

    return result;
}

使用例:

List<int> numbers = new List<int>() { 30, 60, 90, 120, 150, 180, 270, 300, 330 };

// split by max difference
numberList.ToRanges(
    (first, previous, current) => current - previous <= 30).ToArray();
// { 30, 180 }
// { 270, 330 }

// split by max range
numberList.ToRanges(
    (first, previous, current) => current - first <= 90).ToArray();
// { 30, 120 }
// { 150, 180 }
// { 270, 330 }

さらに、整数だけでなく、たとえば単語を最初の文字で分割することもできます。またはDateTime/ TimeSpan. またはあなたが望むものは何でも。

于 2013-06-05T01:43:46.580 に答える
1

これは、1 つの linq ステートメントで実行できます。

var numberList = new List<int>() { 30, 60, 120, 150, 270, 300, 330 };
var section = 0;
var result = numberList
            .Select( (x, i) => new {value = x, section = (i == 0 ? 0 : ((x - numberList[i - 1]) > 30 ? ++section : section))})
            .GroupBy(x => x.section)
            .Select(x => x.Select(v => v.value).ToList()).ToList();
于 2013-06-04T23:04:43.520 に答える
0

TakeWhile結果を使用して別のリストに追加できます

void SplitByRange()
{
    List<int> numberList = new List<int>() { 30, 60, 120, 150, 270, 300, 330 }; 
    IEnumerable<int> aux = new List<int>();

    int n = numberList.First();
    int skip = 0;
    List<List<int>> output = new List<List<int>>();

    while ((aux = numberList.Skip(skip).TakeWhile(o => { bool r = (o - n) <= 30; n = o; return r; })).Count() > 0)
    {
        output.Add(aux.ToList());
        skip += aux.Count();
    }
}

最後numberListは空outputになり、リストのリストになります。

output[0]  // { 30, 60 }
...

現在のコードには、リストに少なくとも 1 つの要素が必要です。

{ 30, 100 }

それぞれに1つの要素を持つ2つのリストとして返されます

{ 30 }
{ 100 }
于 2013-06-04T22:46:00.390 に答える
0

LINQ を使用する必要がありますか? そうでない場合は、次のようになります。

List<int> numberList = new List<int>() { 30, 60, 120, 150, 270, 300, 330 };  

Dictionary<int, int> result = new Dictionary<int, int>();
int lastStart = numberList.First();
for(int i=1; i < numberList.Count; i++)
{
    if(numberList[i] >= lastStart + 30)
    {
        result.Add(lastStart, numberList[i]);
        if (i == numberList.Count - 1) break;
        lastStart = numberList[i + 1];
        i++;
    }
}

foreach (var item in result)
{
    Console.WriteLine("{{{0}, {1}}}", item.Key, item.Value);
}
于 2013-06-04T22:51:11.507 に答える
0

これを試して:

private static List<int[]> GetGroups(List<int> numberList)
{
    List<List<int>> groups = new List<List<int>>();
    numberList.Zip(numberList.Skip(1), (a, b) =>
    {
        if ((b - a) == 30)
        {
            if (groups.Count == 0)
                groups.Add(new List<int>());
            groups[groups.Count - 1].Add(a);
        }
        else if (a == b)
        {
            groups[groups.Count - 1].Add(a);
        }
        else
        {
            groups[groups.Count - 1].Add(a);
            groups.Add(new List<int>());
        }
        return a;
    }).ToList();
    groups[groups.Count - 1].Add(numberList.Last());
    return groups.Select(g => new[] { g.First(), g.Last() }).ToList();
}

使用例:

//List<int> numberList = new List<int>() { 30, 60, 90, 120, 150, 180, 270, 300, 330 };
List<int> numberList = new List<int>() { 30, 60, 120, 150, 270, 300, 330 };
var result = GetGroups(numberList);
于 2013-06-04T22:52:56.420 に答える