2014-09-03 105 views
3

的n个我要生成从这些文字字符串,没有字符串被复制并在这里复制意味着每个字符串必须包含唯一的话笛卡尔产品列表

例如,如果一旦产生“你好吗”,那么“你是如何'不应该被考虑在结果中'。

我可以有任意数量的名单

e.g

List1 List2 List3 List4 List5 
word11 word21 word21 word21 word51 
word12 word22 word22 word22 word52 
word13 word23 word23 word23 word53 
word14 word24 word24 word24 word54 
word15 word25 word25 word25 word55 

这些列表会在AllSimilarWordsLists以复加。我想使用笛卡尔产品生成字符串列表。已经找到this但这个解决方案有固定数量的列表,任何人有想法。

+0

这么一句话可以出现在多个列表,每个生成的字符串必须包含每个列表的东西吗? – Chris 2014-09-03 11:44:24

+0

是的,你的权利@chris – 2014-09-03 11:49:31

回答

8

不幸的是,我不记得在那里我发现它

public static IEnumerable<IEnumerable<T>> CartesianProduct<T> 
    (this IEnumerable<IEnumerable<T>> sequences) 
{ 
    IEnumerable<IEnumerable<T>> emptyProduct = 
     new[] { Enumerable.Empty<T>() }; 
    IEnumerable<IEnumerable<T>> result = emptyProduct; 
    foreach (IEnumerable<T> sequence in sequences) 
    { 
     result = from accseq in result from item in sequence select accseq.Concat(new[] {item}); 
    } 
    return result; 
} 
+0

源可能是http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx - 不完全相同但非常接近。 – Chris 2014-09-03 11:45:30

+0

是的,我认为,你是对的。埃里克Lippert是非常值得信赖的来源使用 – Vladmir 2014-09-03 12:57:05

+0

这是不工作任何想法? – MonsterMMORPG 2016-04-10 13:58:16