我有一些不同的集合,这些集合由一些有价值的名称标识。我使用类似Dictionary<string, List<string>>
的构造来存储这样的实体。
作为我的输出,我需要可以从此输入语法生成的所有可能的值组合。在我的尝试中,我设计了一种基于索引的方法,这样如果索引被正确地操作,我就得到类似于示例的表单的必需输出。我知道它可能会使用递归,但我不确定如何去做。这是公认的问题吗?请在下面找到代码:
using System;
using System.Collections.Generic;
namespace Rextester
{
public class Program
{
private static Dictionary<string, string> dataAtIndex(Dictionary<string, List<string>> iArgs, Dictionary<string, int> iArgIndex)
{
Dictionary<string, string> oConditionKeys = new Dictionary<string, string>();
foreach (var vArgName in iArgs.Keys)
{
int index = iArgIndex[vArgName];
string argValue = iArgs[vArgName][index];
oConditionKeys.Add(vArgName, argValue);
}
return oConditionKeys;
}
private static void testFunc(Dictionary<string, List<string>> iArgs, ref List<Dictionary<string, string>> oListSelectKeys)
{
Dictionary<string, int> argIndex = new Dictionary<string, int>();
foreach (var vArgName in iArgs.Keys)
{
argIndex.Add(vArgName, 0);
}
oListSelectKeys.Add(dataAtIndex(iArgs, argIndex));
}
public static void Main(string[] mainargs)
{
Dictionary<string, List<string>> args = new Dictionary<string, List<string>>();
List<string> listArgData1 = new List<string>();
listArgData1.Add("1=>Arg0");
listArgData1.Add("1=>Arg1");
listArgData1.Add("1=>Arg2");
List<string> listArgData2 = new List<string>();
listArgData2.Add("2=>Arg0");
listArgData2.Add("2=>Arg1");
List<string> listArgData3 = new List<string>();
listArgData3.Add("3=>Arg0");
listArgData3.Add("3=>Arg1");
listArgData3.Add("3=>Arg2");
listArgData3.Add("3=>Arg3");
args.Add("Param1", listArgData1);
args.Add("Param2", listArgData2);
args.Add("Param3", listArgData3);
Console.WriteLine("Input Data : ");
foreach(var vKey in args.Keys) {
Console.Write(vKey + " : ");
foreach(var vData in args[vKey]) {
Console.Write(vData + " ");
}
Console.WriteLine();
}
Console.WriteLine();
List<Dictionary<string, string>> listSelectKeys = new List<Dictionary<string, string>>();
testFunc(args, ref listSelectKeys);
int count = 0;
Console.WriteLine("Output Data : ");
foreach(var vElem in listSelectKeys) {
Console.Write(count++ + " : ");
foreach(var vKey in vElem.Keys) {
Console.Write("(" + vKey + ", " + vElem[vKey] + ") ");
}
Console.WriteLine();
}
}
}
}
输出应该是:
0 : (Param1, 1=>Arg0) (Param2, 2=>Arg0) (Param3, 3=>Arg0)
1 : (Param1, 1=>Arg0) (Param2, 2=>Arg0) (Param3, 3=>Arg1)
2 : (Param1, 1=>Arg0) (Param2, 2=>Arg0) (Param3, 3=>Arg2)
...
n : (Param1, 1=>Arg2) (Param2, 2=>Arg1) (Param3, 3=>Arg3)
谢谢!
答案 0 :(得分:0)
终于搞定了...... :) 我用了一些循环递归。:) 你走了:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Rextester
{
public class Program
{
private static Dictionary<string, string> dataAtIndex(Dictionary<string, List<string>> iArgs, Dictionary<string, int> iArgIndex)
{
Dictionary<string, string> oConditionKeys = new Dictionary<string, string>();
foreach (var vArgName in iArgs.Keys)
{
int index = iArgIndex[vArgName];
string argValue = iArgs[vArgName][index];
oConditionKeys.Add(vArgName, argValue);
}
return oConditionKeys;
}
private static void generateArgIndexList(Dictionary<string, List<string>> iArgs, Dictionary<string, int> iArgIndex, ref List<Dictionary<string, int>> oListArgIndices)
{
if(iArgs.Count > 0)
{
string firstArgName = iArgs.First().Key;
Dictionary<string, List<string>> newArgs = new Dictionary<string, List<string>>(iArgs);
Dictionary<string, int> tmpArgInd = iArgIndex;
while (tmpArgInd[firstArgName] < iArgs[firstArgName].Count)
{
string lastArgName = newArgs.Last().Key;
if (tmpArgInd[lastArgName] < iArgs[lastArgName].Count)
{
oListArgIndices.Add(new Dictionary<string, int>(tmpArgInd));
++tmpArgInd[lastArgName];
}
else
{
tmpArgInd[lastArgName] = 0;
newArgs.Remove(lastArgName);
lastArgName = newArgs.Last().Key;
if (tmpArgInd[lastArgName] < iArgs[lastArgName].Count)
{
++tmpArgInd[lastArgName];
if (tmpArgInd[lastArgName] < iArgs[lastArgName].Count)
generateArgIndexList(iArgs, tmpArgInd, ref oListArgIndices);
}
}
}
}
}
private static void testFunc(Dictionary<string, List<string>> iArgs, ref List<Dictionary<string, string>> oListConditionKeys)
{
Dictionary<string, int> argIndex = new Dictionary<string, int>();
foreach (var vArgName in iArgs.Keys)
{
argIndex.Add(vArgName, 0);
}
List<Dictionary<string, int>> listArgIndices = new List<Dictionary<string, int>>();
generateArgIndexList(iArgs, argIndex, ref listArgIndices);
foreach(var vArgIndex in listArgIndices)
{
oListConditionKeys.Add(dataAtIndex(iArgs, vArgIndex));
}
}
public static void Main(string[] mainargs)
{
Dictionary<string, List<string>> args = new Dictionary<string, List<string>>();
List<string> listArgData1 = new List<string>();
listArgData1.Add("1=>Arg0");
listArgData1.Add("1=>Arg1");
listArgData1.Add("1=>Arg2");
List<string> listArgData2 = new List<string>();
listArgData2.Add("2=>Arg0");
listArgData2.Add("2=>Arg1");
List<string> listArgData3 = new List<string>();
listArgData3.Add("3=>Arg0");
listArgData3.Add("3=>Arg1");
listArgData3.Add("3=>Arg2");
listArgData3.Add("3=>Arg3");
args.Add("Param1", listArgData1);
args.Add("Param2", listArgData2);
args.Add("Param3", listArgData3);
Console.WriteLine("Input Data : ");
foreach(var vKey in args.Keys) {
Console.Write(vKey + " : ");
foreach(var vData in args[vKey]) {
Console.Write(vData + " ");
}
Console.WriteLine();
}
Console.WriteLine();
List<Dictionary<string, string>> listSelectKeys = new List<Dictionary<string, string>>();
testFunc(args, ref listSelectKeys);
int count = 0;
Console.WriteLine("Output Data : ");
foreach(var vElem in listSelectKeys) {
Console.Write(count++ + " : ");
foreach(var vKey in vElem.Keys) {
Console.Write("(" + vKey + ", " + vElem[vKey] + ") ");
}
Console.WriteLine();
}
}
}
}