0

最初のキーには2つの値があり、2番目のキーにdict<string, list<string>>,は3つの値があり、3番目のキーには3つの値があります。各値セットから値を取得すると、2*3*3 = 18 セットの組み合わせになります c# でコーディングする方法は?

ありがとう

編集申し訳ありませんが明確にしませんでした

私はこのようなものが欲しい

 {"1",new List<String>(){"a", "b"}}, 
    {"2",new List<String>(){"c", "d", "e"}}, 
 {"3", new List<string>() {"f", "g"}

この acf、acg、adf、adg、aef、aeg bcf、bcg、bdf、bdg、bef、beg のような出力が必要です

4

5 に答える 5

1

Linq を使用:

var dict = new Dictionary<String, List<String>>() { 
    {"1",new List<String>(){"a", "b"}},
    {"2",new List<String>(){"c", "d", "e"}},
    {"3",new List<String>(){"f", "g", "h"}},
};
var combis = from kv in dict
             from val1 in kv.Value
             from val2 in kv.Value
             select string.Format("{0}{1}", val1, val2);
foreach (var combi in combis)
    Console.WriteLine(combi);

デモ: http://ideone.com/nm7mY

于 2012-09-18T20:32:50.807 に答える
0

クイック&ダーティですが、このメソッドを磨くことができます. 結果リストには、期待される結果が含まれています。

使用法:

var dict = new Dictionary<String, List<String>>() { 
    {"1",new List<String>(){"a", "b"}},
    {"2",new List<String>(){"c", "d", "e"}},
    {"3",new List<String>(){"f", "g"}},
};

var collections = dict.Select(kvp => kvp.Value).ToArray();            
var result = new List<string>(); 
GetNextProduct(collections, 0, String.Empty, result);

結果を生成するメソッド:

private static void GetNextProduct(IEnumerable<string>[] collections, int collectionIndex, string currentProduct, IList<string> results)
{
    var currentList = collections[collectionIndex];
    bool isLast = collections.Length == collectionIndex + 1;
    foreach (var s in currentList)
    {
        if (isLast) results.Add(currentProduct + s);
        else GetNextProduct(collections, collectionIndex + 1, currentProduct + s, results);
    }
}
于 2012-09-18T21:08:14.247 に答える
0

私はあなたがこれを意味すると思いますか?

Dictionary<string, int> dict = new Dictionary<string, int>
{
    { "Hello World", 1 },
    { "HelloWorld", 1 },
    { "Hello  World", 1 },
};

foreach (var item in dict) // var is of type KeyValuePair<string, int>
    Console.WriteLine(item.Key + ", " + item.Value);
于 2012-09-18T20:28:29.523 に答える
0
        Dictionary<string, List<int>> storage = new Dictionary<string, List<int>>();
        storage.Add("key1", new List<int>() { 2, 7 });
        storage.Add("key2", new List<int>() { 8, 4, 1});
        storage.Add("key3", new List<int>() { 3, 9, 3 });
        foreach (string key in storage.Keys)
        {
            //access to single storage...
            List<int> subStorage = (List<int>)storage[key];
            foreach (int item in subStorage)
            {
                //access to single value inside storage...
            }
        }
于 2012-09-18T20:31:04.260 に答える
0

リスト内の値を読み取ったり編集したりしようとしている場合は、次のようなことを試します。

Dictionary<int, List<string>> dict = new Dictionary<int, List<string>>();
var arrayOfValues = dict.Values.ToArray();

for (int i = 0; i < arrayOfValues.Length; i++)
{
    for (int j = 0; j < arrayOfValues[i].Count; j++)
    {
        //read/edit arrayOfValues[i][j];
    }
}

「ツリー」の部門を知っているので、再帰は必要ありません。

于 2012-09-18T20:45:43.937 に答える