利用String [] []中String []的每个排列创建字典复合键 [英] Creating a Dictionary Composite Key out of Every Permutation in a String[] within a String[][]
问题描述
我有一个Dictionary<string,string>
用作另一个Dictionary
的组合键.
I have a Dictionary<string,string>
that is acting as the composite key for another Dictionary
.
我还有一些string[]
数组,其中包含用作复合键的必要字符串.
I also have some string[]
arrays that contains the necessary strings to be used as the composite key.
例如:
//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>
的CompositeKey,例如:
I would like to make the composite key out of every possible combination pair of strings within each array. For example, I'd like to fill the compositeKey thats a Dictionary<string, string>
with every permutation of the array1 string[]
...Such as:
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
我尝试过的是这种嵌套循环算法,它似乎可以按正确的顺序获取我想要的排列.
What I have tried is this nested loop algorithm that seems to work for getting the permutations I want in the right order.
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
完全错误.
The section within the try catch block is where I am having trouble. I can't seem to create the compositeKey
correctly. Is this because of the nested loop, or am I using dictionarys
completely wrong.
似乎我试图以错误的方式使用字典.关于在此处应使用的数据类型有什么建议吗?
It would seem that I am trying to use dictionary in a wrong way. Is there any suggestion on the type of datatype that should be used here?
任何建议都值得赞赏.
推荐答案
在创建所有可能的组合时,您正在寻找一个笛卡尔联接,对于您自己的情况,它是一个数组 .可以借助 Linq
When creating all possible combinations, you are looking for a Cartesian Join, in your case an array with itself. It can be easily implemented with a help of 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[]
用作字典中的键 GetHashCode
和Equals
:
Please, notice, that you shouldn't use string[]
as a key in a dictionary since array's implementation doesn't override GetHashCode
and Equals
:
// 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]
这篇关于利用String [] []中String []的每个排列创建字典复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!