N-Ary/多列表笛卡尔积 [英] N-Ary/Multiple-List Cartesian Product
本文介绍了N-Ary/多列表笛卡尔积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在产品SKU中,我们可以通过LINQ来获得笛卡尔积:
in Product SKUs, we can get Cartesian Product in the way of LINQ:
string[] arr1 = new[] {"red", "blue", "orange"};
string[] arr2 = new[] {"5 inche", "8 inch"};
var result = from a in arr1
from b in arr2
select a + " " + b;
foreach (var item in result){
Console.WriteLine(item);
}
我们知道数组的确切数量.
We know the exact number of arrays.
我的问题是:如果数组的数量是动态的,我们如何获得笛卡尔积?
My question is: If the number of arrays is dynamic, how do we get Cartesian Product?
谢谢.
推荐答案
我会将它们放在字符串数组的list
中.然后,我将使用 ForEach
:
I'd put them in a list
of string arrays. Then I'd use ForEach
:
IEnumerable<string> result = list.First();
list.RemoveAt(0);
list.ForEach(delegate(IEnumerable<string> value)
{
result = (from r in result
from v in value
select r + " " + v).ToList();
});
您还可以看到此链接:面向小组成员的笛卡尔积
You can also see this link : Cartesian Product for Group members
这篇关于N-Ary/多列表笛卡尔积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文