【问题标题】:c# combination of List of List items [duplicate]c#列表项列表的组合[重复]
【发布时间】:2017-04-03 10:45:02
【问题描述】:

我有一个List<TeamName>,其中TeamName 类包含List<MembersName>

所以我有这样的事情:

List<TeamName> teamNameList //each item of "tnl" has public member "memberNameList"

...

class TeamName //each team has different member
{
   public TName  
   public List<MemberName> memberNameLis;
} 

class MemberName
{
     public MName;
}

现在我想对每个团队的 MemberName 进行所有可能的组合。

例如我有 3 个团队 (T1, T2, T3),他们的成员数为 -

T1 = 3 个成员,T2 = 2 个成员,T3 = 1 个成员,

所以现在我需要一些东西 -

T1M1 T2M1 T3M1

T1M1 T2M2 T3M1

T1M2 T2M1 T3M1

T1M2 T2M2 T3M1

T1M3 T2M1 T3M1

T1M3 T2M2 T3M1

List<List<MemberName>> combi;

如何在代码中做到这一点? 我只是一个初学者,所以请原谅我没有以干净的方式提出问题。 请帮忙:)

【问题讨论】:

  • 组合:T1M1 T2M1 T3M1 T1M1 T2M2 T3M1 T1M2 T2M1 T3M1 T1M2 T2M2 T3M1 T1M3 T2M1 T3M1 T1M3 T2M2 T3M1
  • 不,这两个问题都不同。每个团队都有不同的成员列表
  • @RaihanAl-maMun,不,我认为它只是笛卡尔积。让我发布答案。
  • 作为副本提出的原始问题具有额外的复杂性,因此更难以在这里看到它的适用性。但这仍然是基本相同的问题。您只需要您的个人团队列表的笛卡尔积。上面标记的副本以类似的方式代表相同的基本问题,Eric 的回答参考了先前提出的副本以获得更详细的解决方案。更一般地说,在 Stack Overflow 上有 很多 的“笛卡尔积”问答,所以如果你在理解这两个方面有困难,只需寻找其他的。

标签: c# list combinations


【解决方案1】:

假设您的团队列表为 -

List<TeamName> tnl;
...
...
...

// now you can call like - 
IEnumerable<IEnumerable<MemberName>> allPossible = CartesianProduct(tnl.Select(c => c.mnl));
...
...

和笛卡尔积方法会一样

IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences)
{
    IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
    return sequences.Aggregate(
        emptyProduct,
        (accumulator, sequence) =>
            from accseq in accumulator
            from item in sequence
            select accseq.Concat(new[] { item })
        );
}

试一试,您的输入将为您提供所需的结果。

如果你会循环 -

 foreach (var item in r4)
 {
     foreach (var mn in item)
     {
         Console.Write("\t"+mn.Name);
     }
     Console.WriteLine();
 }

你会得到答案 -

T1M1    T2M1    T3M1
T1M1    T2M2    T3M1
T1M2    T2M1    T3M1
T1M2    T2M2    T3M1
T1M3    T2M1    T3M1
T1M3    T2M2    T3M1

我已经测试过了。

【讨论】:

    【解决方案2】:

    解决办法

    List<TeamName> teamNameList = new List<TeamName>();
    teamNameList.Add(new TeamName() { TName = "T1",
        memberNameList = new List<MemberName> {
            new MemberName { MName = "M1" },
            new MemberName { MName = "M2" },
            new MemberName { MName = "M3" } } });
    teamNameList.Add(new TeamName() { TName = "T2",
        memberNameList = new List<MemberName> {
            new MemberName { MName = "M1" },
            new MemberName { MName = "M2" } } });
    teamNameList.Add(new TeamName() { TName = "T3",
        memberNameList = new List<MemberName> { new MemberName { MName = "M1" } } });
    
    List<List<MemberName>> combi = new List<List<MemberName>>();
    //indexes keep index of each member for each team
    List<int> indexes = new List<int>(teamNameList.Count);
    //initialize them to the first members
    for (int i = 0; i < teamNameList.Count; i++)
        indexes.Add(0);
    
    //We will enumerate all combinations in this loop
    while (true)
    {
        //Create a new entry for combi
        List<MemberName> members = new List<MemberName>();
        //Select respective member based on the current index value for each team
        for (int i = 0; i < teamNameList.Count; i++)
            members.Add(new MemberName() { MName = teamNameList[i].TName + teamNameList[i].memberNameList[indexes[i]].MName });
    
        combi.Add(members);
    
        //Increment indexes and go to the next combination
        for (int i = indexes.Count - 1; i >= 0; i--)
        {
            indexes[i]++;
            //If we enumerated all members of the current team - start from the beginning of that members
            //Else we should proceed with the next member
            if (indexes[i] == teamNameList[i].memberNameList.Count)
                indexes[i] = 0;
            else
                break;
        }
    
        //If we enumerated all combinations than all indexes will become zero because of the previous loop
        if (indexes.Sum() == 0)
            break;
    }
    

    【讨论】:

    • 添加了一些 cmets
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多