gpt4 book ai didi

c# - 如何使用 LINQ 将多级元组列表拆分为多级值列表?

转载 作者:太空宇宙 更新时间:2023-11-03 22:29:39 25 4
gpt4 key购买 nike

我有一个 List<List<Tuple<int, int, int>>>变量,我想把它分成三个 List<List<int>>秒。我已经可以像这样枚举列表三次:

List<List<Tuple<int, int, int>>> tuples = ...
List<List<int>> a = tuples.Select(x => x.Select(y => y.Item1).ToList()).ToList();
List<List<int>> b = tuples.Select(x => x.Select(y => y.Item2).ToList()).ToList();
List<List<int>> c = tuples.Select(x => x.Select(y => y.Item3).ToList()).ToList();

但是,我可以使用 LINQ 一次创建所有三个列表吗?像这样的东西:

Tuple<List<List<int>>, List<List<int>>, List<List<int>>> splitted = tuples.Magic(...);

编辑:(这是在tymtam和Mong Zhu发布他们的答案后添加的)

更清楚地说,稍后当我说“马上”时,我指的是性能。我真的不在乎结果的实际类型,我也不痴迷于单行完成。

性能方面的比较:

这是我的代码,用于比较拆分 1000 个包含 1000 个元组的列表,(50 次调用,4000 个样本/秒):

private void Split_Loop(List<List<Tuple<int, int, int>>> tuples)
{
List<List<int>> a = new List<List<int>>(tuples.Count);
List<List<int>> b = new List<List<int>>(tuples.Count);
List<List<int>> c = new List<List<int>>(tuples.Count);
for (int i = 0; i < tuples.Count; i++)
{
int n = tuples[i].Count;
List<int> l1 = new List<int>(n);
a.Add(l1);
List<int> l2 = new List<int>(n);
b.Add(l2);
List<int> l3 = new List<int>(n);
c.Add(l3);

for (int j = 0; j < n; j++)
{
l1.Add(tuples[i][j].Item1);
l2.Add(tuples[i][j].Item2);
l3.Add(tuples[i][j].Item3);
}
}
}
private void Split_Linq3(List<List<Tuple<int, int, int>>> tuples)
{
List<List<int>> a = tuples.Select(x => x.Select(y => y.Item1).ToList()).ToList();
List<List<int>> b = tuples.Select(x => x.Select(y => y.Item2).ToList()).ToList();
List<List<int>> c = tuples.Select(x => x.Select(y => y.Item3).ToList()).ToList();
}
private void Split_tymtam(List<List<Tuple<int, int, int>>> tuples)
{
IEnumerable<List<List<int>>> a = new Func<Tuple<int, int, int>, int>[]
{ t => t.Item1, t => t.Item2, t => t.Item3 }.
Select(f => tuples.Select(x => x.Select(f).ToList()).ToList());
List<List<List<int>>> b = a.ToList();
}
private void Split_Mong(List<List<Tuple<int, int, int>>> tuples)
{
Func<List<List<Tuple<int, int, int>>>, Func<Tuple<int, int, int>, int>, List<List<int>>> selectorGen = (x, t) => x.Select(y => y.Select(t).ToList()).ToList();
Tuple<List<List<int>>, List<List<int>>, List<List<int>>> splitted = Tuple.Create(
selectorGen(tuples, t => t.Item1),
selectorGen(tuples, t => t.Item2),
selectorGen(tuples, t => t.Item3));
}
private void button3_Click(object sender, EventArgs e)
{
int outer = 1000;
int inner = 1000;
List<List<Tuple<int, int, int>>> tuples = new List<List<Tuple<int, int, int>>>(outer);
for (int i = 0; i < outer; i++)
{
List<Tuple<int, int, int>> list = new List<Tuple<int, int, int>>(inner);
tuples.Add(list);
for (int j = 0; j < inner; j++)
{
Tuple<int, int, int> tuple = new Tuple<int, int, int>(i + j + 1, i + j + 2, i + j + 3);
list.Add(tuple);
}
}
for (int i = 0; i < 50; i++)
{
Split_Loop(tuples);
Split_Linq3(tuples);
Split_tymtam(tuples);
Split_Mong(tuples);
}
}

结果如下: enter image description here

最佳答案

这是我能做的最好的了:

IEnumerable<List<List<int>>> myLists = new Func<Tuple<int, int, int>, int>[] 
{ t => t.Item1, t => t.Item2, t => t.Item3 }
.Select(f => tuples.Select(x => x.Select(f).ToList()).ToList());

关于c# - 如何使用 LINQ 将多级元组列表拆分为多级值列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58589590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com