问题
I have a Dictionary<string,string>
that is acting as the composite key for another Dictionary
.
I also have some string[]
arrays that contains the necessary strings to be used as the composite key.
For example:
//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};
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
and for the second array:
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
{
}
}
}
}
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.
Edit: 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?
Any advice is appreciated.
回答1:
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();
Test:
string test = string.Join(Environment.NewLine, arrayOfArrays
.Select(line => $"[{string.Join(", ", line)}]"));
Console.WriteLine(test);
Outcome:
[aa, aa]
[aa, bb]
[aa, cc]
[bb, aa]
[bb, bb]
[bb, cc]
[cc, aa]
[cc, bb]
[cc, cc]
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!");
You may want to choose Tuple<string, string>
instead:
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));
Outcome:
[(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]
来源:https://stackoverflow.com/questions/47709881/creating-a-dictionary-composite-key-out-of-every-permutation-in-a-string-withi