Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

c# - Generate List with Every Combination between two variables

How can I auto generate a List<string> with every combination between A00B00 to A100B100?

I think it would be 10,100 items.

So the List would contain:

A00B00
A00B01
A00B02
...
A01B00
A01B01
A01B02
...
A100B98
A100B99
A100B100

It needs to keep the AxxBxx format. I do not want it to be scrambled like 0A01B0.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since you have a range of 0-100 and you just have A and B as a prefix, We populate two lists each with ints from 0 to 100 and then use System.Linq to find all combinations and append then to a list with a delimiter. Now just replace the delimiter.

There are a bunch of ways you can go about it, I would recommend using System.Linq

public static IEnumerable<string> FindAllCombinationsAnotherWay()
{
    List<int> ListOne = Enumerable.Range(1, 100).ToList();
    List<int> ListTwo = Enumerable.Range(1, 100).ToList();

    var result = ListOne.SelectMany((a, indexA) =>
             ListTwo.Where((b, indexB) =>
                    ListTwo.Contains(a) ? !b.Equals(a) && indexB > indexA
                                      : !b.Equals(a))
                  .Select(b => string.Format("A{0:00}B{1:00}", a, b)));

    return result.Distinct();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...