Generate all combinations for a list of strings

前端 未结 4 921
情歌与酒
情歌与酒 2020-12-05 08:14

I want to generate a list of all possible combinations of a list of strings (it\'s actually a list of objects, but for simplicity we\'ll use strings). I need this list so th

4条回答
  •  生来不讨喜
    2020-12-05 09:00

    If you want all variations, have a look at this project to see how it's implemented.

    http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G

    But you can use it since it's open source under CPOL.

    For example:

    var allValues = new List() { "A1", "A2", "A3", "B1", "B2", "C1" };
    List result = new List();
    var indices = Enumerable.Range(1, allValues.Count);
    foreach (int lowerIndex in indices)
    {
        var partVariations = new Facet.Combinatorics.Variations(allValues, lowerIndex);
        result.AddRange(partVariations.Select(p => String.Join(" ", p)));
    }
    
    var length = result.Count;  // 1956
    

提交回复
热议问题