首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >创建所有可能组合的列表

创建所有可能组合的列表
EN

Stack Overflow用户
提问于 2012-02-03 19:53:23
回答 5查看 3K关注 0票数 2

我试着做以下几件事。

我想要创建一个列表,列出某些事情之间所有可能的关系。

例如。玛丽,爱丽丝,琼,辛迪,伊丽莎白,贝蒂,贾克斯

我想为这样的列表创建所有可能的组合:

  • 玛丽,爱丽丝
  • 玛丽,六月
  • 玛丽辛迪
  • 玛丽,贾克斯
  • 玛丽,爱丽丝,琼
  • 玛丽,爱丽丝,辛迪
  • 玛丽爱丽丝伊丽莎白..。
  • 玛丽,爱丽丝,贾克斯
  • 玛丽,琼,辛迪
  • 玛丽琼伊丽莎白..。
  • 玛丽,琼,贾克斯
  • 玛丽,辛迪,伊丽莎白
  • 玛丽,辛迪,贝蒂
  • 玛丽辛迪贾克斯。
  • 玛丽,爱丽丝,琼,辛迪
  • 玛丽,爱丽丝,琼,伊丽莎白
  • 玛丽爱丽丝琼贝蒂。
  • 玛丽,爱丽丝,琼,辛迪,伊丽莎白
  • 玛丽,爱丽丝,琼,辛迪,贝蒂

有谁知道在SQL、Access或C#中这样做的方法吗?如果有另一种语言,我可以使用DB的,我会非常感激!

谢谢你,玛丽亚

EN

回答 5

Stack Overflow用户

发布于 2012-02-03 20:37:39

您可能喜欢许多现代DB服务器为此使用的递归查询。

ACCESS不是其中之一:(

下面是一个使用门柱的示例

代码语言:javascript
运行
复制
postgres=# with RECURSIVE y1(b,c,d) as (
postgres(#      with x1(a) as (
postgres(#              values('a')
postgres(#              union all
postgres(#              values ('b')
postgres(#              union all
postgres(#              values ('c')
postgres(#              union all
postgres(#              values ('d')
postgres(#      )
postgres(#      select a,a,1
postgres(#      from x1
postgres(#      union all
postgres(#      select a||b,a,d+1
postgres(#      from x1
postgres(#              join y1 on (a < c)
postgres(# )
postgres-# select *
postgres-# from y1;
  b   | c | d
------+---+---
 a    | a | 1
 b    | b | 1
 c    | c | 1
 d    | d | 1
 ab   | a | 2
 ac   | a | 2
 ad   | a | 2
 bc   | b | 2
 bd   | b | 2
 cd   | c | 2
 abc  | a | 3
 abd  | a | 3
 acd  | a | 3
 bcd  | b | 3
 abcd | a | 4
(15 rows)


postgres=#
票数 4
EN

Stack Overflow用户

发布于 2012-02-03 20:10:10

SQL可以非常好地处理这类事情。如果你想走得很远,它就会变粘,但如果你知道你想要最多五个项目的所有组合:

代码语言:javascript
运行
复制
DECLARE @things TABLE (n nvarchar(50));

INSERT INTO @things (n) VALUES ('Mary'),('Alice'),('June'),('Cindy'),('Elizabeth'),('Betty'),('Jax'), (null);

SELECT 
  ISNULL(t1.n + ',', '') 
  + ISNULL(t2.n + ',', '') 
  + ISNULL(t3.n+ ',', '') 
  + ISNULL(t4.n+ ',', '') 
  + ISNULL(t5.n, '') 
FROM @things AS t1
JOIN @things AS t2 ON 1=1
JOIN @things AS t3 ON 1=1
JOIN @things AS t4 ON 1=1
JOIN @things AS t5 ON 1=1
票数 2
EN

Stack Overflow用户

发布于 2012-02-03 20:13:13

如果您指的是功率集,那么您可以在C#中使用以下内容

代码语言:javascript
运行
复制
public IEnumerable<IEnumerable<T>> GetPowerSet<T>(List<T> list)
{
    return from m in Enumerable.Range(0, 1 << list.Count)
           select
             from i in Enumerable.Range(0, list.Count)
             where (m & (1 << i)) != 0
             select list[i];
}

用法:

代码语言:javascript
运行
复制
var names = new List<string> { "Mary", "Alice", "June", "Cindy", "Elizabeth", "Betty", "Jax" };
var powerSet = GetPowerSet(names);
foreach (var nameCollection in powerSet)
{
  foreach (var name in nameCollection)
  {
    Console.Write(name);
  }
  Console.WriteLine();
}

您可能希望删除少于2个名称的任何集合,以及完整的名称集:

代码语言:javascript
运行
复制
var cleaned = powerSet.Where(nc => nc.Count() > 1 && nc.Count() < names.Count());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9134650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档