一个String [] []内的String []创建一个字典复合键出每种排列的(Creating

2019-09-28 19:58发布

我有一个Dictionary<string,string>即充当另一个复合键Dictionary

我也有一些string[]包含要用作复合键中的必要的字符串数组。

例如:

//Dictionary that will use composite key
Dictionary<Dictionary<string, string>, decimal> lookUpDictionary = new Dictionary<Dictionary<string, string>, decimal>();

//The composite key dictionary
Dictionary<string, string> compositeKey = new Dictionary<string, string>();

//Array containing strings
string[] array1 = { "aa", "bb", "cc" };
string[] array2 = { "xx", "yy", "zz" };

//A container for the array of individual units
string[][] arrayOfArrays = {array1,array2};

我想使该复合键从每个阵列内的每个可能的组合对字符串的。 例如,我想以填充compositeKey那是一个Dictionary<string, string>与ARRAY1的每个排列string[] ...如:

aa aa
aa bb
aa cc
bb aa
bb bb
bb cc
cc aa
cc bb
cc cc

以及用于所述第二阵列:

xx xx
xx yy
xx zz
yy xx
yy yy
yy zz
zz xx
zz yy
zz zz

我已经试过的是,似乎为获得我想要的排列正确的顺序工作,此嵌套循环算法。

Dictionary<string, string> compositeKey = new Dictionary<string, string>();

string[] array1 = { "aa", "bb", "cc" };
string[] array2 = { "xx", "yy", "zz" };

string[][] arrayOfArrays = {array1,array2};


//for every string[] in the arrayofarrays[][]
foreach(string[] array in arrayOfArrays)
{
    //for every string in each of those string[] arrays in the arrayOfArrays[][]
    foreach(string word in array)
    {
        //get every combination including itself
        foreach(string wordsPair in array)
        {
            string[] permutation = { word, wordsPair };
            Console.WriteLine(permutation[0] + permutation[1]);
            try
            {
                //compositeKey.Add(permutation[0], permutation[1]);
                //string value = compositeKey[permutation[0]];
                //Console.WriteLine(value);
            }
            catch
            {

            }


        }

    }
}

在try catch块内的部分中,我有麻烦。 我似乎无法创建compositeKey正确。 这是因为嵌套循环的,还是我使用dictionarys完全错误的。

编辑:这似乎是我想使用字典以错误的方式。 是否有应该在此处使用的数据类型的类型的任何建议?

任何建议表示赞赏。

Answer 1:

当创建所有可能的组合,你正在寻找一个笛卡尔加入 ,你的情况与自身的数组。 它可以使用LINQ的帮助可以轻松实现

  string[] array1 = { "aa", "bb", "cc" };
  //string[] array2 = { "xx", "yy", "zz" };

  string[][] arrayOfArrays = array1
    .SelectMany(left => array1, (left, right) => new string[] { left, right })
    .ToArray();

测试:

  string test = string.Join(Environment.NewLine, arrayOfArrays
    .Select(line => $"[{string.Join(", ", line)}]"));

  Console.WriteLine(test);

结果:

[aa, aa]
[aa, bb]
[aa, cc]
[bb, aa]
[bb, bb]
[bb, cc]
[cc, aa]
[cc, bb]
[cc, cc]

请通知,你不应该使用 string[]作为字典的关键 ,数组的实现没有重写 GetHashCodeEquals

   // Don't do this! Do not use string[] as a key...
   Dictionary<string[], string> demo = new Dictionary<string[], string>() {
     {new string[] {"a", "b"}, "c"},
   };

   string[] key = new string[] {"a", "b"};     

   // ... And that's the reason why:
   if (!demo.ContainsKey(key))
     Console.WriteLine("Oops!");

你可能想选择Tuple<string, string>代替:

  string[] array1 = { "aa", "bb", "cc" };

  Dictionary<Tuple<string, string>, string> dictionary = array1
    .SelectMany(left => array1, (left, right) => new Tuple<string, string>(left, right))
    .ToDictionary(item => item, 
                  item => "value for " + string.Join(" & ", item.Item1, item.Item2));

  Console.WriteLine(string.Join(Environment.NewLine, dictionary));

结果:

[(aa, aa), value for aa & aa]
[(aa, bb), value for aa & bb]
[(aa, cc), value for aa & cc]
[(bb, aa), value for bb & aa]
[(bb, bb), value for bb & bb]
[(bb, cc), value for bb & cc]
[(cc, aa), value for cc & aa]
[(cc, bb), value for cc & bb]
[(cc, cc), value for cc & cc]


文章来源: Creating a Dictionary Composite Key out of Every Permutation in a String[] within a String[][]