笛卡尔产品列表的n个 [英] Cartesian products with n number of list
问题描述
我有
List<List<string>> AllSimilarWordsLists { get; set; }
我要生成从这些文字字符串,没有字符串被复制并在这里复制是指每每一个字符串必须包含唯一字
I want to generate string from these words such that no string is duplicate and Here duplicate means each and every string must contain unique words
例如,如果一旦产生'你',那么'你是如何不应该在结果中得到考虑。
e.g if once generated 'How are you' then 'are you how' should not be considered in result'.
我可以有任意数量的名单
I can have any number of list
例如
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以复加。我想生成使用笛卡尔积字符串列表。有没有发现这但这种方法是有名单的固定数,得到任何的想法。
These list are going to be added in AllSimilarWordsLists. I want to generate list of string using cartesian products . Have found this but this solution is having fix number of lists, Anybody having ideas.
推荐答案
不幸的是,我不记得在那里我发现它
Unfortunately I don't remember where I found it
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;
}
这篇关于笛卡尔产品列表的n个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!