Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
824 views
in Technique[技术] by (71.8m points)

c# - Generate all Combinations from Multiple (n) Lists

EDIT: I am completely redoing my questions as I have figured out the simplest way of asking it. Thanks to the commenters so far that got me thinking about the root problem.

public List<string> GetAllPossibleCombos(List<List<string>> strings)
{
    List<string> PossibleCombos = new List<string>();

    //????
    {
        string combo = string.Empty;
        // ????
        {
            combo += ????
        }
        PossibleCombos.Add(combo);
    }

    return PossibleCombos;
}

I need to figure out how to recursively go through each List<string> and combine 1 string from each list into a combo string. Don't worry too much about formatting the string as the "live" code uses a custom object instead. Also, feel free to assume that every list will contain at least 1 string and that there are no null values.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a simple non-recursive solution that just concatenates the elements of each combination:

public static List<string> GetAllPossibleCombos(List<List<string>> strings)
{
    IEnumerable<string> combos = new [] { "" };

    foreach (var inner in strings)
        combos = from c in combos
                 from i in inner
                 select c + i;

    return combos.ToList();
}

static void Main(string[] args)
{
    var x = GetAllPossibleCombos(
        new List<List<string>>{
            new List<string> { "a", "b", "c" },
            new List<string> { "x", "y" },
            new List<string> { "1", "2", "3", "4" }});
}

You could generalize this to return an IEnumerable<IEnumerable<string>>, which allows the caller to apply any operation they like for transforming each combination into a string (such as the string.Join below). The combinations are enumerated using deferred execution.

public static IEnumerable<IEnumerable<string>> GetAllPossibleCombos(
    IEnumerable<IEnumerable<string>> strings)
{
    IEnumerable<IEnumerable<string>> combos = new string[][] { new string[0] };

    foreach (var inner in strings)
        combos = from c in combos
                 from i in inner
                 select c.Append(i);

    return combos;
}

public static IEnumerable<TSource> Append<TSource>(
    this IEnumerable<TSource> source, TSource item)
{
    foreach (TSource element in source)
        yield return element;

    yield return item;
}

static void Main(string[] args)
{
    var combos = GetAllPossibleCombos(
        new List<List<string>>{
            new List<string> { "a", "b", "c" },
            new List<string> { "x", "y" },
            new List<string> { "1", "2", "3", "4" }});

    var result = combos.Select(c => string.Join(",", c)).ToList();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...