0

「組み合わせ」という用語が正しいかどうかは完全にはわかりませんが、1 つ以上のリストから組み合わせのリストを作成する必要があります。各リストにはさまざまな数の要素が含まれます。

List<string> lBag1 = ["1_0, 1_1, 1_3"]
List<string> lBag2 = ["11_0, 11_1, 11_8"]
List<string> lBag3 = ["3_0"]

私が必要とするのは、リスト形式の 1 から n 要素までのすべての組み合わせで、各リストから 1 つ以下の要素が含まれます。

"1_0"
"1_1"
"1_3"
"11_0"
"11_1"
"11_8"
"3_0"
"1_0 11_0"
"1_0 11_1"
"1_0 11_8"
"1_0 3_0"
...
"1_3 11_8 3_0"

順序は重要ではないため、「1_0 11_0」は「11_0 1_0」と同じと見なされます。

どんな援助でも大歓迎です

4

3 に答える 3

1

これら 2 つの拡張メソッドを使用すると、複数の列挙を連鎖させて、必要な組み合わせを計算できます。

各組み合わせは、連結された文字列ではなく、列挙です。

// This method takes two sequences of T, and returns
//  - each element of the first sequence,
//        wrapped in its own one-element sequence
//  - each element of the second sequence,
//        wrapped in its own one-element sequence
//  - each pair of elements (one from each sequence),
//        as a two-element sequence.
// e.g. { 1 }.CrossWith({ 2 }) returns { { 1 }, { 2 }, { 1, 2 } }
public static IEnumerable<IEnumerable<T>> CrossWith<T>(
    this IEnumerable<T> source1,
    IEnumerable<T> source2)
{
    foreach(T s1 in source1) yield return new[] { s1 };
    foreach(T s2 in source2) yield return new[] { s2 };
    foreach(T s1 in source1)
        foreach(T s2 in source2)
            yield return new[] { s1, s2 };
}

// This method takes a sequence of sequences of T and a sequence of T,
//     and returns
//  - each sequence from the first sequence
//  - each element of the second sequence,
//        wrapped in its own one-element sequence
//  - each pair, with the element from the second sequence appended to the
//        sequence from the first sequence.
// e.g. { { 1, 2 } }.CrossWith({ 3 }) returns
//      { { 1, 2 }, { 3 }, { 1, 2, 3 } }
public static IEnumerable<IEnumerable<T>> CrossWith<T>(
    this IEnumerable<IEnumerable<T>> source1,
    IEnumerable<T> source2)
{
    foreach(IEnumerable<T> s1 in source1) yield return s1;
    foreach(T s2 in source2) yield return new[] { s2 };
    foreach(IEnumerable<T> s1 in source1)
        foreach(T s2 in source2)
            yield return s1.Concat(new[] { s2 }).ToArray();
}

var cross = lBag1.CrossWith(lBag2).CrossWith(lBag3);
// { "1_0" }, { "1_1" }, { "1_3" } ...
// ... { "1_0", "11_0" }, ...

または、同様のことを行う古典的な Eric Lippertのブログ投稿があります。(同様の結果、非常に異なる方法。)

于 2012-09-18T09:11:27.317 に答える
1

これはあなたのために働きますか:

var empty = new string[] { null, };

var query =
    from b1 in empty.Concat(lBag1)
    from b2 in empty.Concat(lBag2)
    from b3 in empty.Concat(lBag3)
    let bs = new [] { b1, b2, b3 }.Where(b => b != null)
    let result = String.Join(" ", bs)
    where result != ""
    select result;
于 2012-09-18T09:06:13.510 に答える
0

これは私の意見です。実装方法は自分で決める必要がありますが、私は次のようにします。

1) ペア X_Y を表すクラスを作成します

2) クラスに IEquatable を実装させる

3) Equal の実装を提供する

4) X_Y 形式の文字列を指定すると、YourClass オブジェクトを返すコンストラクタを実装します。

5) X_Y のコンマ区切りリストを含む文字列が与えられた場合、リストを返す public static 関数を実装します。

6) 前の方法を使用して 3 つのリストを作成します。

7) 空のリストを作成する

8) yourList.Append を使用して、3 つのリストから要素を追加します。

ハエを殺すために銃を使っているのかもしれません。

于 2012-09-18T09:11:44.180 に答える