我有一个List<int>
(List<List<int>>
)的列表。该列表由比赛中开始号码的博彩组成。假设我在第一场比赛中选择了三个选项,然后在剩下的六场比赛中选择了一个选项。这加起来有三种下注组合。
包含三个唯一行(下注)的List<List<int>>
将如下所示:
[0] 1 [0] 2 [0] 3
[1] 1 [1] 1 [1] 1
[2] 1 [2] 1 [2] 1
[3] 1 [3] 1 [3] 1
[4] 1 [4] 1 [4] 1
[5] 1 [5] 1 [5] 1
[6] 1 [6] 1 [6] 1
当我提交下注时,我希望将单个下注分组为较小的分组下注(可能是List<List<string>>
?)它在数学上表示了上面的列表,所以根据上面的例子:
[0] "1,2,3"
[1] "1"
[2] "1"
[3] "1"
[4] "1"
[5] "1"
[6] "1"
我已经找到了与笛卡尔乘积主题相关的类似解决方案。但似乎没有一种方法适用于这个问题。有什么想法吗?
我找到了这个Reverse Cartesian Product,它描述了我的问题,但我真的不能将这个答案从java翻译成c#。
编辑:只是为了澄清评论中的问题,每个赌注总是由7场比赛组成,所以如果我在第2-7场比赛中选择1号赌注,然后在第一场比赛中选择1号,2号,3号。我的函数使用.Count == 7创建了三行列表。我只是想说明列表的内容。在c#中,用户可以这样启动一个新的列表:
List<List<int>> list = new List<List<int>> { new List<int> { 1, 1, 1, 1, 1, 1, 1 }, new List<int> { 2, 1, 1, 1, 1, 1, 1 }, new List<int> { 3, 1, 1, 1, 1, 1, 1 } };
发布于 2019-11-06 21:42:10
似乎您想要删除每一行上重复的int值(= top list的item ),但保留行本身,即使它们是彼此重复的。
这可以通过使用Select()
和Distinct()
来实现
var bets = new List<List<int>>()
{
new List<int>() { 1 , 2 , 3 },
new List<int>() { 1 , 1 , 1 },
new List<int>() { 1 , 1 , 1 },
new List<int>() { 1 , 1 , 1 },
new List<int>() { 1 , 1 , 1 },
new List<int>() { 1 , 1 , 1 },
new List<int>() { 1 , 1 , 1 },
}
var reducedBets = bets.Select(bet => bet.Distinct());
reducedBets
现在实际上等于:
new List<List<int>>()
{
new List<int>() { 1 , 2 , 3 },
new List<int>() { 1 },
new List<int>() { 1 },
new List<int>() { 1 },
new List<int>() { 1 },
new List<int>() { 1 },
new List<int>() { 1 },
}
我不清楚你在答案中使用的符号(奇怪的索引用法),但我推断这就是你想要的?
发布于 2019-11-06 21:42:10
而不是0,1,2,3,4,5,...你可以用1,2,4,8,16,...这样,您可以使用逐位或运算符(或简单地对它们求和)来组合下注:
[0] 1 | 2 | 4 (equals 7)
[1] 1 | 1 | 1 (equals 1)
要知道如果它只包含一个元素,您可以使用逐位and (&
),因此
if(bet & 4 == 4) it contains 4;
if(bet & 8 == 8) it contains 8;
诸若此类。
因此包含1,8,16下注为(1+8+16或1|8|16 = 25);
0
表示不下注。
所以你仍然可以在一个int中拥有所有的下注组合。
发布于 2019-11-07 00:19:03
你似乎想要的东西可以通过LINQ来完成:
var lists = new List<List<int>>()
{
new List<int> { 1, 1, 1, 1, 1, 1, 1 },
new List<int> { 2, 1, 1, 1, 1, 1, 1 },
new List<int> { 3, 1, 1, 1, 1, 1, 1 }
};
var result = lists
.SelectMany(i => i.Select((value, index) => new { value, index }), (list, value) => value)
.GroupBy(tuple => tuple.index)
.Select(grouping => grouping.Select(tuple => tuple.value).Distinct().ToList()).ToList();
首先,我们将每个值转换为一个包含值和它的索引的对象(Select
),然后我们扁平这个列表,这样我们就得到了一个包含所有条目的巨大列表(SelectMany
)。之后,我们根据它们的索引(GroupBy
)对它们进行分组,并从这些分组中选择值(Select
),在这一点上,我们确保删除所有重复项(Distinct
),并将所有这些不同的值放入一个列表(ToList
)中,然后将所有这些列表放入结果列表(ToList
)
https://stackoverflow.com/questions/58731387
复制相似问题