C# 从列表中获取可能的 DISTINCT 组合
C# get possible DISTINCT combinations from list
可能已经有答案了,我只是在使用错误的术语进行搜索。如果是这样,我深表歉意,如果确实有答案,请指点我现有的答案。
我需要的是能够获取一个列表并从该列表中生成所有可能的不同组合。我所说的不同是指相同的组合不应以不同的顺序出现。
这是一个例子:
假设我有一个包含值 "One" "Two" 和 "Three" 的列表,那么我希望输出为 = "One","Two","Three", "One, Two","Two Three","One Two Three"
我对"blank"组合没兴趣,也不想return同一个组合出现在多个订单中。我发现并试图为我工作的大部分代码都有一个主要缺陷,即它将 return 所有可能性,包括 "Two three" 和 "Three two",这在我的情况下是错误的。
我目前一直在努力改编的代码来自另一个 SO 问题 (All Possible Combinations of a list of Values),看起来像这样:
public void GetCombination(System.Collections.Generic.List<string> list)
{
Combinations = new List<string>();
double count = System.Math.Pow(2, list.Count);
for (int i = 1; i <= count - 1; i++)
{
string str = Convert.ToString(i, 2).PadLeft(list.Count, '0');
for (int j = 0; j < str.Length; j++)
{
if (str[j] == '1')
{
Combinations.Add(list[j]);
}
}
///Combinations = found;
}
}
不幸的是,我是 C# 的新手,老实说,我使用它只是因为一位同事提到它可能比我当前的 powershell 脚本快得多,它可以完成我正在寻找的东西。 ..但是要花很长时间。
在此先感谢您的建议!
下面是使用 the power sets answer Martin Smith 提到的代码的实现:
class Program {
static void Main(string[] args) {
// find all possible combinations of this list
var input = new [] {"One", "Two", "Three"};
var output = FastPowerSet(input);
Print(output);
Console.ReadLine();
}
static T[][] FastPowerSet<T>(T[] seq) {
var powerSet = new T[1 << seq.Length][];
powerSet[0] = new T[0]; // starting only with empty set
for (var i = 0; i < seq.Length; i++) {
var cur = seq[i];
var count = 1 << i; // doubling list each time
for (var j = 0; j < count; j++) {
var source = powerSet[j];
var destination = powerSet[count + j] = new T[source.Length + 1];
for (var q = 0; q < source.Length; q++)
destination[q] = source[q];
destination[source.Length] = cur;
}
}
return powerSet;
}
static void Print<T>(T[][] seq) {
for (var i = 0; i < seq.Length; i++) {
var line = new StringBuilder();
for (var j = 0; j < seq[i].Length; j++ ) {
line.AppendFormat("{0}, ", seq[i][j]);
}
Console.WriteLine(line);
}
}
}
可能已经有答案了,我只是在使用错误的术语进行搜索。如果是这样,我深表歉意,如果确实有答案,请指点我现有的答案。
我需要的是能够获取一个列表并从该列表中生成所有可能的不同组合。我所说的不同是指相同的组合不应以不同的顺序出现。
这是一个例子: 假设我有一个包含值 "One" "Two" 和 "Three" 的列表,那么我希望输出为 = "One","Two","Three", "One, Two","Two Three","One Two Three"
我对"blank"组合没兴趣,也不想return同一个组合出现在多个订单中。我发现并试图为我工作的大部分代码都有一个主要缺陷,即它将 return 所有可能性,包括 "Two three" 和 "Three two",这在我的情况下是错误的。
我目前一直在努力改编的代码来自另一个 SO 问题 (All Possible Combinations of a list of Values),看起来像这样:
public void GetCombination(System.Collections.Generic.List<string> list)
{
Combinations = new List<string>();
double count = System.Math.Pow(2, list.Count);
for (int i = 1; i <= count - 1; i++)
{
string str = Convert.ToString(i, 2).PadLeft(list.Count, '0');
for (int j = 0; j < str.Length; j++)
{
if (str[j] == '1')
{
Combinations.Add(list[j]);
}
}
///Combinations = found;
}
}
不幸的是,我是 C# 的新手,老实说,我使用它只是因为一位同事提到它可能比我当前的 powershell 脚本快得多,它可以完成我正在寻找的东西。 ..但是要花很长时间。
在此先感谢您的建议!
下面是使用 the power sets answer Martin Smith 提到的代码的实现:
class Program {
static void Main(string[] args) {
// find all possible combinations of this list
var input = new [] {"One", "Two", "Three"};
var output = FastPowerSet(input);
Print(output);
Console.ReadLine();
}
static T[][] FastPowerSet<T>(T[] seq) {
var powerSet = new T[1 << seq.Length][];
powerSet[0] = new T[0]; // starting only with empty set
for (var i = 0; i < seq.Length; i++) {
var cur = seq[i];
var count = 1 << i; // doubling list each time
for (var j = 0; j < count; j++) {
var source = powerSet[j];
var destination = powerSet[count + j] = new T[source.Length + 1];
for (var q = 0; q < source.Length; q++)
destination[q] = source[q];
destination[source.Length] = cur;
}
}
return powerSet;
}
static void Print<T>(T[][] seq) {
for (var i = 0; i < seq.Length; i++) {
var line = new StringBuilder();
for (var j = 0; j < seq[i].Length; j++ ) {
line.AppendFormat("{0}, ", seq[i][j]);
}
Console.WriteLine(line);
}
}
}