gpt4 book ai didi

c# - 从集合中获取不同的组合

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:14:57 27 4
gpt4 key购买 nike

我有不同的集合,这些集合由一些有值的名称标识。我使用类似 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)

谢谢!

最佳答案

终于让它工作了...:)我使用了一些循环递归。:)给你:

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();
}
}
}
}

关于c# - 从集合中获取不同的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48617062/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com