您可以使用Enumerable.SelectMany来展平嵌套列表:
List<int> flattened = allLists.SelectMany(l => l).ToList();
Run Code Online (Sandbox Code Playgroud)
是否可以将扁平列表展开回嵌套列表?
您可以使用a Tuple<int, int>来存储原始列表Item1的编号和编号本身Item2.
// create sample data
var allLists = new List<List<int>>() {
new List<int>(){ 1,2,3 },
new List<int>(){ 4,5,6 },
new List<int>(){ 7,8,9 },
};
List<Tuple<int, int>> flattened = allLists
.Select((l, i) => new{ List = l, Position = i + 1 })
.SelectMany(x => x.List.Select(i => Tuple.Create(x.Position, i)))
.ToList();
// now you have all numbers flattened in one list:
foreach (var t in flattened)
{
Console.WriteLine("Number: " + t.Item2); // prints out the number
}
// unflatten
allLists = flattened.GroupBy(t => t.Item1)
.Select(g => g.Select(t => t.Item2).ToList())
.ToList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1507 次 |
| 最近记录: |