在 String[][] 中的 String[] 中的每个排列中创建字典复合键

Creating a Dictionary Composite Key out of Every Permutation in a String[] within a String[][]

我有一个 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};

我想用每个数组中每对可能的字符串组合来制作复合键。例如,我想用 array1 string[] 的每个排列填充 Dictionary<string, 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 完全错误。

编辑:看来我试图以错误的方式使用字典。对此处应使用的数据类型有什么建议吗?

如有任何建议,我们将不胜感激。

在创建所有可能的组合时,您正在寻找一个 笛卡尔连接,在您的例子中是一个数组自身。它可以在 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[]作为字典中的,因为数组的实现不't 覆盖 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]