将2个或更多数组连接成新列表中的唯一组合

时间:2013-04-15 18:56:34

标签: c# arrays string list

我正在解决一个问题,即我可以拥有2个或更多字符串值数组。

从第一个数组开始,我需要获取每个值并连接下一个数组的第一个值和第三个数据中的第一个,依此类推,直到所有可能的组合合并为止。

示例:

Array1 {'A', 'B'}
Array2 {'C', 'D', 'E'}
Array3 {'F', 'G', 'H'}

输出

Row 1 = A, C, F
Row 2 = A, C, G
Row 3 = A, C, H
Row 4 = A, D, F
Row 5 = A, D, G
Row 6 = A, D, H

依此类推,直到完成所有组合。在这种情况下,它将是18种组合。

我之前使用过字符串连接来组合这些值,但从来没有在这样的过程中,数组的数量可以改变,并且产生这种输出类型的项目数量。

3 个答案:

答案 0 :(得分:2)

Here就是答案

List<string[]> lists = new List<string[]>()
{
    new[]{"A", "B"},
    new[]{"C", "D", "E"},
    new[]{"F", "G", "H"}
};

var cp = lists.CartesianProduct();

foreach(var line in cp)
{
    Console.WriteLine(String.Join(" ",line));
}

public static partial class MyExtensions
{
    //http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx
    public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
    {
        // base case: 
        IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
        foreach (var sequence in sequences)
        {
            var s = sequence; // don't close over the loop variable 
            // recursive case: use SelectMany to build the new product out of the old one 
            result =
                from seq in result
                from item in s
                select seq.Concat(new[] { item });
        }
        return result;
    }
}

答案 1 :(得分:0)

嵌套for循环适用于此:

for (int i=0;i<=Array1.Length;i++)
{
  for (int j=0; j<=Array2.Length; j++)
  {
    for (int k=0;k<=Array3.Length; k++)
      //output however Array1[i] + ", " + Array2[j] + ", " + Array3[k];
  }
}

答案 2 :(得分:0)

您可以使用Cartesian Product算法列出所有组合:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CartesianProduct
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] array1 = new char[] { 'A', 'B' };
            char[] array2 = new char[] { 'C', 'D', 'E' };
            char[] array3 = new char[] { 'F', 'G', 'H' };

            int iterations = array1.Length * array2.Length * array3.Length;

            for (int i = 0; i < iterations; i++)
            {
                Console.WriteLine("{0}, {1}, {2}", array1[i % array1.Length], array2[i % array2.Length], array3[i % array3.Length]);
            }
            Console.WriteLine("Total iterations: " + iterations.ToString());
            Console.Read();
        }
    }
}