C#LINQ组合:没有空集的集合的所有组合

use*_*287 7 c# linq combinatorics

我有一组字符串,我想找到所有可能的字符串组合并将它们添加到列表中.我想最终得到一个列表,列出每个字符串组合,减去空集.

我已经创建了一个解决方案,它完全使用嵌套的for循环.但是我想更优雅地做到这一点,最好用LINQ,我不是很精通,因为我还是很新的.

解决方案应该有2 ^ n - 1个组合列表,其中n是原始集合的基数.这是我正在寻找的正确例子:

set = {a, b, c}

completedListOfCombinations = 
{
    {a},
    {b},
    {a, b},
    {c},
    {a, c},
    {b, c},
    {a, b, c}
}
Run Code Online (Sandbox Code Playgroud)

这是我的工作,基本但丑陋的解决方案,我在以下帮助下制作:https://stackoverflow.com/a/3319652/3371287

List<string> myStrings =  new List<string> { "a", "b", "c" };

var allCombos = new List<List<string>>();

for (int i = 0; i < myStrings.Count; i++)
{
    int subsetCount = allCombos.Count;
    var m = new List<string>();
    m.Add(myStrings[i]);
    allCombos.Add(m);

    for (int j = 0; j < subsetCount; j++)
    {
        string[] subset = new string[allCombos.ElementAt(j).Count + 1];
        allCombos[j].CopyTo(subset, 0);
        subset[subset.Length - 1] = myStrings[i];
        allCombos.Add(subset.ToList());
    }

}
Run Code Online (Sandbox Code Playgroud)

有人能告诉我更优雅的解决方案吗?我已经看到类似的LINQ解决方案创建了具有阈值的笛卡尔对和列表,但我无法将它们调整到我需要的位置.

Dmi*_*nko 14

假设其中的所有list都是唯一的:

  List <String> list = new List<String> { "a", "b", "c" };

  var result = Enumerable
    .Range(1, (1 << list.Count) - 1)
    .Select(index => list.Where((item, idx) => ((1 << idx) & index) != 0).ToList());
Run Code Online (Sandbox Code Playgroud)

要打印出来:

Console.WriteLine(String
  .Join(Environment.NewLine, result
     .Select(line => String.Join(", ", line))));
Run Code Online (Sandbox Code Playgroud)

结果是

a
b
a, b
c
a, c
b, c
a, b, c
Run Code Online (Sandbox Code Playgroud)

  • 您可以将其视为从1到2 ^ n-1计数.如果你看二进制索引(001 - > 010 - > 011 - > 100 - > 101 - > 110 - > 111),那么将每个二进制数字映射到你的一个列表值("a","b"," c"),并且只显示1(我将0显示为0而不显示它们),然后你得到(00a,0b0,0ba,c00,c0a,cb0,cba) (4认同)