列出所有可能的组合 [英] List all the possible combinations

查看:124
本文介绍了列出所有可能的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与该组合的问题。

I have a question related to the combination.

我实际开发一个电子商务网站,我有一个功能,允许用户创建产品变型。

I am actually developing a ecommerce site and I have a function to allow customer create product variant.

例如:黑色裤子34W 30L,黑色裤子38W 32L,白裤子34W30升。这些被定义为变型产品。

For example: Black Pant 34W 30L, Black Pant 38W 32L, White Pant 34W 30l. Those are defined as product variants.

假设我的裤子有3个选项,它们的颜色,腰围尺寸和长度。

Assume my pant has 3 options, and they are color, waist size and length.

我现在有3个表。

ListA = {"black", "white", "red"} //For the color
ListB = {30,32,34,36,38} //For the waist
ListC ={28,30,32,34} //For the length

我的问题是我怎么能列出所有可能的组合?

My question is how can I list all the possible combinations?

我想要的结果应该看起来像{{黑,30,28},{黑,30,30},{黑,30,32},{白,34 ,30}}

My desired result should be look like {{black,30,28},{black,30,30},{black,30,32},{white,34 ,30}}

P.S。最棘手的部分是,我不知道有多少选择的客户将如何分配给本产品。选项​​的计数可能只是1,这是最简单的;它可能是超过3 ...

P.S. The tricky part is that I don't know how many options customer will assign to this product. The count of the option might be just 1, which is the easiest; it could be more than 3...

问题解决了

由于我们不知道有多少的选择,我们都会有。因此,我们不知道有多少的循环中,我们将使用。换句话说,它变成一个典型的笛卡尔积。

Since we don't know how many options we will have. Therefore, we don't know how many for loop we are going to use. In other word, it turns to a typical Cartesian Products.

有关详细信息,可从以下两个链接阅读。 http://www.interact-sw.co .UK / iangblog / 2010/07/28 / LINQ笛卡尔-1 <一href="http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx" rel="nofollow">http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx

For more information, you can read from these two links. http://www.interact-sw.co.uk/iangblog/2010/07/28/linq-cartesian-1 http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx

感谢您对您的帮助!

推荐答案

正如评论指出埃里克利珀有一个名为博客中的计算一个乘积LINQ 说明如何解决你的问题。你需要一个扩展的方法来计算笛卡尔乘积:

As stated in the comments Eric Lippert has a blog post named Computing a Cartesian Product with LINQ that explains how to solve your problem. You need an extension method to compute the cartesian product:

public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) {
  IEnumerable<IEnumerable<T>> result = new [] { Enumerable.Empty<T>() };
  foreach (var sequence in sequences) {
    var localSequence = sequence;
    result = result.SelectMany(
      _ => localSequence,
      (seq, item) => seq.Concat(new[] { item })
    );
  }
  return result;
}

然后,你需要序列的一个序列,以通过执行该产品。你的情况,你有你的序列,两个字符串和整数这样的公共基类型 T 必须对象

var sequences = new[] {
  new Object[] { "black", "white", "red" },
  new Object[] { 30, 32, 34, 36, 38 },
  new Object[] { 28, 30, 32, 34 }
};

要计算笛卡尔乘积您只需调用扩展方法:

To compute the Cartesian product you simply invoke the extension method:

var result = sequences.CartesianProduct();

在枚举的结果是计算上飞(懒洋洋)。如果preFER创建列表,你需要了ToList() Concat的,也之前调用列表返回结果的扩展方法。

When you enumerate the result it is computed on the fly (lazily). If you prefer to create a list of lists you need to call ToList() after Concat and also before returning result from the extension method.

这篇关于列出所有可能的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆